Calculating Time differences in milliseconds

  • Thread starter Thread starter DBLWizard
  • Start date Start date
D

DBLWizard

Howdy,

I have looked and don't see a straight forward way to calculate time
differences in milliseconds.

What I am wanting to do is benchmark or time how long things take. So
I want to record the time ... do something ... record the time and see
how long the "do something" took.

Am I just being hairbrained here and its right in front of me or what?

Thanks

dbl
 
DBLWizard,

Basically, when you want to find out how long something takes, store the
value of DateTime.Now in a variable, and then at the end of the operation,
subtract that from the current DateTime.Now value. This will give you a
TimeSpan instance from which you can find out how many milliseconds have
elapsed.

In .NET 2.0, you will want to use the Stopwatch class (I believe it is
in System.Diagnostics), as it will use the performance counter and give you
much more accurate results (as well as finer resolution, should you need
it).

Hope this helps.
 
DateTime start = DateTime.Now;
......
......
DateTime finish = DateTime.Now;
System.TimeSpan diff = finish - start;

string milliseconds = diff.TotalMilliseconds.ToString();


--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.
 
dbl,
It is right in front of you ;)
The difference between two DateTimes gives you a Timespan object.
The Timespan object has a "TotalMilliseconds" property.

:)
Andrew Arace
 
Back
Top