How do I calculate elapsed time in milliseconds?

  • Thread starter Thread starter JSmith
  • Start date Start date
J

JSmith

I want to determine the elapsed time between two events. What is the best
way to do this?
 
JSmith,

The best way would be to get the value returned by the static Now
property on the DateTime class when the first event fires. When the second
event fires, get the time returned from the Now property again. Then, you
can take the difference between the two.

Hope this helps.
 
try this:


DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;
// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Milliseconds ;


Vladimir
 
Yes, this is true, but keep in mind that its not an instantanious
operation. i.e its based CPU load and the timeslice that the current
thread gets, that the Now operation is dependent on. It will probably be
off by only millis but off nevertheless.
 
Why not just use the Environment.TickCount property. Get the value when the
first and second event fires, then subtract the first value from the second.
That'll give you the difference in milliseconds.
 
Not sure if this does what you want, but I like using unix time stamp in
general:

double my_start_time, my_end_time, my_diff;
my_start_time = (DateTime.UtcNow - new
DateTime(1970,1,1,0,0,0)).TotalSeconds;

// for loop or some events goes in here

my_end_time = (DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds;

my_diff = my_end_time-my_start_time;
 
Back
Top