471,306 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

C# code benchmark performance

Hi,

I have various chunks of code i want to benchmark to see how long they take
to do.

What i'd like to do is check the time at start of code then check time at
end of time to see how long it took. I need better accuracy than just
milliseconds

I'm looking for the time to be very accurate and to give similar results to
the
QueryPerformanceFrequency();
QueryPerformanceCounter();

that i have used in a previous C++ program.

This can give values accurate to microseconds.

Thanks In Advance
Macca
Dec 6 '05 #1
6 2078
System.TimeSpan TS;

DateTime nw = DateTime.Now;

................ your code ...........

TS = new TimeSpan(DateTime.Now.Ticks - nw.Ticks);

System.Diagnostics.Debug.WriteLine("This took " +
Convert.ToString(TS.TotalMilliseconds) + " ms");

Hope this helps :)

Enjoy,

Nick.

Software News Blog: http://www.mysoftwareblog.com/
Personal Blog: http://viri.demon.nl/blog/

Dec 6 '05 #2
You called also do PInvoke and use
QueryPerformanceFrequency();
QueryPerformanceCounter();

Dec 6 '05 #3
Or if you're using 2.0 the System.Diagnostics.Stopwatch class is
provided.

BravesCharm wrote:
You called also do PInvoke and use
QueryPerformanceFrequency();
QueryPerformanceCounter();


Dec 6 '05 #4
Hmm, seems i didn't read your question properly, you can replace
TotalMilliSeconds with Ticks to get the result in ticks (computer
cannot be more specific then ticks when time in concerned if i can
remember correctly).

Dec 6 '05 #5
ViRi wrote:
Hmm, seems i didn't read your question properly, you can replace
TotalMilliSeconds with Ticks to get the result in ticks (computer
cannot be more specific then ticks when time in concerned if i can
remember correctly).


Use QueryPerformanceCounter(), I think that's the best you can get
(resolution is microseconds or even less, depending on the
QueryPerformanceFrequency()). You'll have to [DllImport] it though.

hth,
Max

Dec 6 '05 #6

Markus Stoeger wrote:
Use QueryPerformanceCounter(), I think that's the best you can get
(resolution is microseconds or even less, depending on the
QueryPerformanceFrequency()). You'll have to [DllImport] it though.


It's the best you can get unless you happen to have some special
hardware device installed. Again, if you're using 2.0 the Stopwatch
class basically wraps this API so you only need to [DllImport] if
you're using 1.1 or lower.

Brian

Dec 6 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Duncan Lissett | last post: by
15 posts views Thread by Duncan Lissett | last post: by
reply views Thread by julio | last post: by
3 posts views Thread by Steve | last post: by
2 posts views Thread by Matthijs van Waveren | last post: by
2 posts views Thread by ke zhang | last post: by
88 posts views Thread by Peter Olcott | last post: by
30 posts views Thread by galiorenye | last post: by
37 posts views Thread by Jack | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.