C# Programming > Miscellaneous

Source Code Speed Test

c# optimize

C# Optimization Utility

This simple C# project will help you when optimizing your C# applications. The application will take a code-snippet and run it through a loop for a specified number of times. Remember to set the number high enough to notice a change in speed.

For tips on improving C# source code performance visit the Code Optimization Article.


The utility measures the speed of the C#.Net code by using the System.Diagnostics StopWatch class. The StopWatch class comes with two functions that we use: Start() and Stop(). Underneath those functions they are using the API call QueryPerformanceTimer, which is a high speed timer of better precision than using DateTime.Now. Here is the basic C# code:

StopWatch timer = new StopWatch();

timer.Start(); //Some code timer.Stop();

TimeSpan speed = new TimeSpan(timer.ElapsedTicks);

.NET Class Version

This code speed measuring technique can be further simplified with the use of delegates to create a source code speed test C# class.


I recommend you work with the utility from inside Visual Studio (or Express Edition). C# applications executed from within the debugging environment run slower which is actually a good thing. In other words it will make it easier to notice speed improvements or fallbacks from tweaking your C# code.

Back to C# Article List