Microsoft KB Archive/306979

= How to create classes and objects in Visual C# =

Article ID: 306979

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q306979



For a Microsoft Visual Basic .NET version of this article, see 306978.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Runtime.InteropServices

IN THIS TASK
SUMMARY
 * Build and Then Run a Demonstration Application
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article describes how to use the QueryPerformanceCounter function to time application code.

When you test code to identify performance bottlenecks, you want to use the highest resolution timer that the system has to offer.

NOTE: JScript .NET cannot call Microsoft Windows API functions.

back to the top

Build and Then Run a Demonstration Application
 Start Visual Studio .NET or Visual Studio 2005 and create a new Visual C# Console application.  Replace the default code with the following code, which times operations in increments of 100: using System; using System.Runtime.InteropServices;

namespace csConPerfCounter {   class Class1 {       [DllImport(&quot;kernel32.dll&quot;)] extern static short QueryPerformanceCounter(ref long x); [DllImport(&quot;kernel32.dll&quot;)] extern static short QueryPerformanceFrequency(ref long x);

static void Main(string[] args) {           long ctr1 = 0, ctr2 = 0, freq = 0; int acc = 0, i = 0; if (QueryPerformanceCounter(ref ctr1)!=0)  // Begin timing. {               for (i=0; i<100; i++) acc++;     // Code being timed. QueryPerformanceCounter(ref ctr2); // Finish timing. Console.WriteLine(&quot;Start Value: &quot; + ctr1); Console.WriteLine(&quot;End Value: &quot; + ctr2); QueryPerformanceFrequency(ref freq); Console.WriteLine(&quot;QueryPerformanceCounter minimum resolution: 1/&quot; + freq + &quot; seconds.&quot;); Console.WriteLine(&quot;100 Increment time: &quot; + (ctr2 - ctr1) * 1.0 / freq + &quot; seconds.&quot;); }           else Console.WriteLine(&quot;High-resolution counter not supported.&quot;);

// Make the console window wait.

Console.WriteLine; Console.Write(&quot;Press Enter to finish ... &quot;); Console.Read; }   } }                      Save the application, and press the F5 key to compile and run the application. The console windows should display output similar to the following: Start Value: 281060816204 End Value: 281060816269 QueryPerformanceCounter minimum resolution: 1/3579545 seconds. 100 Increment time: 1.81587324646009E-05 seconds.

Press Enter to finish ...  Press ENTER to stop running the application and to close the Console window.

back to the top

Troubleshooting

 * This API call may fail under some circumstances. Check the return value and adjust your code to make sure that you receive valid results.
 * For best results, test the application multiple times with no other applications or server processes running. Activities in other threads and processes can affect the percentage of time that the system spends in the target application.

back to the top

