By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Calculating Time differences in milliseconds

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DBLWizard" <ib*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 16 '05 #2

P: n/a
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.

"DBLWizard" <ib*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 16 '05 #3

P: n/a
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

"DBLWizard" wrote:
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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.