Results 1 to 5 of 5

Thread: [RESOLVED] Measure time it takes to execute a function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Resolved [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?
    Thanks

    Tomexx.

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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();
    Thanks

    Tomexx.

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [RESOLVED] Measure time it takes to execute a function

    Try something more like this:
    csharp Code:
    1. using System.Diagnostics;
    2.  
    3.  
    4. Stopwatch sw = new Stopwatch();
    5. sw.Start();
    6.  
    7. //run the code to time
    8.  
    9. sw.Stop();
    10. 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
  •  



Click Here to Expand Forum to Full Width