October 2009
M T W T F S S
    Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Categories

  • [+]ASP.NET (7) 
  • [—]C# (87) 
  • [+]C++ (13) 
  • [+]Delphi (20) 
  • [+]JavaScript (24) 
  • [+]Regular Expressions (7) 
  • [+]SQL (13) 
  • [+]VB (100) 
  • [+]VB.NET (4) 

Online

Users: 10 Guests, 1 Bot
  • Loading...


    Loading...

    Login






    Register | Lost password?

    Register





    A password will be mailed to you.
    Log in | Lost password?

    Retrieve password





    A confirmation mail will be sent to your e-mail address.
    Log in | Register
  • Benchmark/performance timers

    Author: Miroslav Stampar

    URL: http://mstampar.awardspace.com/

    // Benchmark/performance timers (See Example tab for usage)
    
      internal class UnmanagedApi
      {
       [DllImport("Kernel32.dll")]
       internal static extern bool QueryPerformanceCounter(
        out long lpPerformanceCount);
    
       [DllImport("Kernel32.dll")]
       internal static extern bool QueryPerformanceFrequency(
        out long lpFrequency);
      }
     public class PrecisionTimer
     {
      private long startTick;
      private long stopTick;
      private long tickFrequency;
      public PrecisionTimer()
      {
       UnmanagedApi.QueryPerformanceFrequency(out tickFrequency);
      }
      internal class UnmanagedApi
      {
       [DllImport("Kernel32.dll")]
       internal static extern bool QueryPerformanceCounter(
        out long lpPerformanceCount);
       [DllImport("Kernel32.dll")]
       internal static extern bool QueryPerformanceFrequency(
        out long lpFrequency);
      }
    
      public void Start()
      {
       Thread.Sleep(0); //execute waiting threads first, then continue
       GC.Collect();
       GC.WaitForPendingFinalizers();
       GC.Collect();
       UnmanagedApi.QueryPerformanceCounter(out startTick);
      }
    
      public void Stop()
      {
       UnmanagedApi.QueryPerformanceCounter(out stopTick);
      }
    
      public static long Tick()
      {
       long retVal;
       UnmanagedApi.QueryPerformanceCounter(out retVal);
       return retVal;
      }
    
      private long ticks
      {
       get
       {
        if (startTick > stopTick)
        {
         startTick = stopTick = 0;
         return 0;
        }
        else
        {
         return (stopTick - startTick);
        }
       }
      }
      public void WriteLine()
      {
       Console.WriteLine("{0} ms", durationMs);
      }
      public double durationS
      {
       get { return (double) ticks/tickFrequency; }
      }
      public double durationMs
      {
       get { return durationS*1000; }
      }
     }
     public class NormalTimer
     {
      private long startTick;
      private long stopTick;
      public void Start()
      {
       Thread.Sleep(0); //execute waiting threads first, then continue
       startTick = Environment.TickCount;
      }
      public void Stop()
      {
       stopTick = Environment.TickCount;
      }
      private long ticks
      {
       get
       {
        if (startTick > stopTick)
        {
         startTick = stopTick = 0;
         return 0;
        }
        else
        {
         return (stopTick - startTick);
        }
       }
      }
    
      public long durationMs
      {
       get { return ticks; }
      }
      public double durationS
      {
       get { return (double) ticks/1000; }
      }
     }
    

    Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • del.icio.us
    • Bloglines
    • Facebook
    • Google Bookmarks
    • LinkedIn
    • Technorati
    • TwitThis
    • Webnews

    Leave a Reply

     

     

     

    You can use these HTML tags

    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">

    Spam Protection by WP-SpamFree Plugin