Welcome to MSDN Blogs
Sign in
|
Join
|
Help
Amol Ravande's Performance blog
Go Faster, Stronger and Higher
Home
Email
RSS 2.0
Atom 1.0
Recent Posts
How to: Create a web test plug-in to extract and store an array of values using Visual Studio Team syste
Debugging OutOfMemoryExceptions in managed code using Windbg
Troubleshooting SQL Deadlocks with Read Committed Isolation using Row versioning
Improving application Start up time: GeneratePublisherEvidence setting in Machine.config
Improving Application performance over WAN
Tags
.NET
Deadlocks
Frontend
OutOfMemoryException
SQL Performance
Startup performance
WAN
Web Test Plug-ins
Web Testing
WinDbg
News
This blog is provided "AS IS" with no warranties, and confers no rights. Opinions are not necessarily of Microsoft.
Blogs I read
Rico's Blog
Maoni's Blog
ACE Team's blog
varun's blog
Alik's blog
Abu's blog
Bani's Blog
Jimmy's blog
Tess's blog
Marks blog
Vance's blog
Alik Levin's Personal Development Blog
Archives
February 2009 (1)
December 2008 (1)
November 2008 (1)
July 2008 (1)
June 2008 (1)
September 2007 (2)
Timing Managed code in .NET
Lets start off with a simple, yet quite useful article on
Timing Managed code
. I will be using this in my future articles to test performance of certain pieces of code. A lot of sites mention the use of WIN32 functions
QueryPerformanceCounter
and
QueryPerformanceFrequency
. They provide timing results with nano-second accuracy. Here's a good article if you guys wanna try out timing managed code using these functions.
http://
msdn2.microsoft.com/en-us/library/ms979201.aspx
However, using these functions is quite inconvenient. Starting with .NET 2.0 comes the
StopWatch
class(also included in .NET 3.0).
Since all Windows systems that support .NET also include a high-resolution performance counter, you can pretty much count on the Stopwatch being a high-resolution timer. If you’re unsure, check the static Stopwatch.IsHighResolution property, which will tell you whether the timer is based on a high-resolution performance counter.
Check out
http://msdn2.microsoft.com/en-us/library/system.diagnostics.stopwatch(vs.80).aspx
for information on StopWatch class. The only problem with StopWatch class is that you need to reset the timer everytime you use start and stop methods.Thats because StopWatch accumulates timing values.
Here is the code
long Frequency, Ticks, TotalTime;
Stopwatch sw = new Stopwatch();
Frequency = Stopwatch.Frequency;
sw.Start();
// your code goes here
sw.Stop();
Ticks = sw.ElapsedTicks;
TotalTime=1000000L*Ticks/Frequency;
Console.WriteLine("Total time in microseconds "+TotalTime);
Here, Frequency is returned as ticks/sec. So if you want time in nano-seconds use 10^9 instead of 10^6 in the TotalTime formula.
Also StopWatch frequency depends on the installed hardware and operating system, hence the
Frequency
value remains constant while the system is running.
Posted:
Saturday, September 15, 2007 7:18 PM by
amolravande
Comments
MSDN Blog Postings » Timing Managed code in .NET
said:
PingBack from
http://msdnrss.thecoderblogs.com/2007/09/15/timing-managed-code-in-net/
#
September 15, 2007 10:21 AM
Anonymous comments are disabled