|
-
Feb 8th, 2008, 01:38 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Measure time it takes to execute a function
In VB6 I used GetTickCount() Windows API. Is there a fuction in c# that will allow me to measure the time it takes to execute a piece of code?
-
Feb 8th, 2008, 02:37 PM
#2
Re: Measure time it takes to execute a function
In 2005/2008 use StopWatch. In 2002/2003 you can either write your own class that uses the QueryPerformanceCounter API or search Google and grab someone else's.
-
Feb 8th, 2008, 02:49 PM
#3
Thread Starter
Hyperactive Member
Re: Measure time it takes to execute a function
Found this and it works:
Code:
/* Read the initial time. */
DateTime startTime = DateTime.Now;
/* Here's the code you're trying to measure */
/* Read the end time. */
DateTime stopTime = DateTime.Now;
/* Compute the duration between the initial and the end time. */
TimeSpan duration = stopTime - startTime;
this.Text = duration.TotalMilliseconds.ToString();
-
Feb 8th, 2008, 02:59 PM
#4
Re: [RESOLVED] Measure time it takes to execute a function
Fyi, while that works, it's horribly inaccurate compared to the System.Diagnostics.StopWatch or the QueryPerformance API.
-
Feb 11th, 2008, 11:53 AM
#5
Re: [RESOLVED] Measure time it takes to execute a function
Try something more like this:
csharp Code:
using System.Diagnostics; Stopwatch sw = new Stopwatch(); sw.Start(); //run the code to time sw.Stop(); MessageBox.Show(sw.ElapsedTime.ToString());
There are a few other fun ways to get the elapsed time with a Stopwatch... my Breakout game is currently using ElapsedMilliseconds for a frame limiter. Fun stuff.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|