Microsoft Knowledge Base Email Alertz

When you test code to identify performance bottlenecks, you want to use the highest resolution timer that the system has to offer. This step-by-step article describes how to use the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

How to create classes and objects in Visual C#

Article ID: 306979 - View products that this article applies to.
This article was previously published under Q306979

On This Page

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.

Build and Then Run a Demonstration Application

  1. Start Visual Studio .NET or Visual Studio 2005 and create a new Visual C# Console application.
  2. 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("kernel32.dll")]
    		extern static short QueryPerformanceCounter(ref long x);
    		[DllImport("kernel32.dll")]
    		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("Start Value: " + ctr1);
    				Console.WriteLine("End Value: " + ctr2);
    				QueryPerformanceFrequency(ref freq);
    				Console.WriteLine("QueryPerformanceCounter minimum resolution: 1" + freq + " seconds.");
    				Console.WriteLine("100 Increment time: " + (ctr2 - ctr1) * 1.0 / freq + " seconds.");
    			}
    			else
    			   Console.WriteLine("High-resolution counter not supported.");
    
    			// Make the console window wait.
    
    			Console.WriteLine();
    			Console.Write("Press Enter to finish ... ");
    			Console.Read();
    		}
    	}
    }
    					
  3. 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 ...
    					
  4. Press ENTER to stop running the application and to close the Console window.

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.

REFERENCES

For more information, search for "QueryPerformanceCounter" and "QueryPerformanceFrequency" in the Online Help.

For more information about other timers, search for "timeGetTime", "GetTickCount", and "System.DateTime class" in the Online Help.

Properties

Article ID: 306979 - Last Review: December 11, 2006 - Revision: 1.4
APPLIES TO
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbperformance KB306979
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate