Click here to Skip to main content
13,451,919 members (44,916 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 26 Feb 2011

How To: Measure execution time in C#

, 1 Mar 2011
Rate this:
Please Sign up or sign in to vote.
How To Measure execution time in C#
Sometimes developers need to measure execution time of task. This method could be useful when is important to compare two or more approaches and choose the best. .NET Framework contains Stopwatch class that can measure elapsed time for one interval, or total elapsed time across multiple intervals.

For more information, navigate to: Stopwatch Class.

The following example demonstrates how to use Stopwatch class to determine execution time for an application. Using this example, you can see differences in efficiency by calling Contains method of StringCollection and HashTable.

Hashtable hs = new Hashtable();
StringCollection sc = new StringCollection();

for (int i = 0; i < int.MaxValue/1000; i++)
    hs.Add(i.ToString(), i);

Random rnd = new Random();
Stopwatch timer = new Stopwatch();

int val = rnd.Next(int.MaxValue / 1000);

if (sc.Contains(val.ToString()))


if (hs.Contains(val.ToString()))


Added by Alex Bell:
This solution by Kanasz Robert is very efficient and adhere to the .NET paradigm of managed code (no "unsafe" code). The similar solution was implemented in the test site dedicated to the Toggle Case Algorithm performance comparison (e.g., see the Ayoola-Bell Algorithm at:[^].

That particular solution in addition to the code block similar to this one, contains the following code snippets, intended to calculate the elapsed time of the algorithm under the test in nanoseconds (might me microseconds, etc.) rather than in ticks, thus extending the capabilities and presenting the result in user-friendly format (ns, us, etc.):

Listing 1.
double frequency = Stopwatch.Frequency;
double nanosecPerTick = (1000 * 1000 * 1000) / frequency;
// return the value in nanosec
double durNanosec = timer.ElapsedTicks * nanosecPerTick;

As the general rule, when testing the extremely fast algorithms (like the Ayoola-Bell Toggle Case, mentioned above with completion time in the range of several microseconds) it's recommended to implement the loop running 10,000...100,000 iterations in order to increase the accuracy of the measurement. Also, be aware of the useful property Stopwatch.IsHighResolution (boolean), which indicates the Stopwatch capability pertinent to the particular HW/OS settings.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kanasz Robert
Architect The Staffing Edge & Marwin Cassovia Soft
Slovakia Slovakia
My name is Robert Kanasz and I have been working with ASP.NET, WinForms and C# for several years.
MCSD - Web Applications
MCSE - Data Platform
MCPD - ASP.NET Developer 3.5
- Web Developer 4
MCITP - Database Administrator 2008
- Database Developer 2008
MCSA - SQL Server 2012
MCTS - .NET Framework 3.5, ASP.NET Applications
- SQL Server 2008, Database Development
- SQL Server 2008, Implementation and Maintenance
- .NET Framework 4, Data Access
- .NET Framework 4, Service Communication Applications
- .NET Framework 4, Web Applications
MS - Programming in HTML5 with JavaScript and CSS3 Specialist

Open source projects: DBScripter - Library for scripting SQL Server database objects

Please, do not forget vote

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 Nice reminder Pin
A.J.Wegierski8-Dec-11 20:15
memberA.J.Wegierski8-Dec-11 20:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180318.3 | Last Updated 1 Mar 2011
Article Copyright 2011 by Kanasz Robert
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid