Testing C# Code Performance Speed

By: VCkicks

Programming with C#, as in a lot of languages, gives an infinite amount of ways to write applications. All the different programming pratices can vary your application's speed and efficiency. Determening which coding techniques are faster is an essential skill.

The algorithm to test out fast and efficient C# source code run is:

-> Initialize variables needed

-> Declare a System.Diagonistic.StopWatch varible.

-> Include this line before starting the test: [StopWatch variable].Start();

-> Setup a for loop with the code to be tested inside

-> The amount of trials should be set up in such a way that the total execute time lasts an appropriate duration

-> Execute this line to stop the StopWatch: [StopWatch variable].Stop();

-> Initialize a new TimeSpan variable with: TimeSpan span = new TimeSpan([StopWatch variable].ElapsedTicks);

-> The span variable has the amount of time the source took to run

An optional part is to divide the time it took to execute the C# code and divide it by the amount of times the for loop ran. Giving you the average time per call.

I myself like to compare the total execution time because it has less chance of variation in the results.

Often the first time the code is run? the source code will run slower than normal, so try to run several tests and? average out the results.

Additionally I recommend to test source code from within Visual Studio since source code that runs with the debugger runs a bit slower. Creating a nicer window of time to compare different snippets of source code.

Remember that the optimized Release version of your program will be optimized by the compiler (inline code and such) and will almost always run faster than the debug version.

You can download a free C# Example Utility that uses the algorithm described at => http://vckicks.110mb.com/code_speed_test.html

Also for general tips on optimizing C# code visit => http://vckicks.110mb.com/optimize_csharp_code.html for 5 ways to quickly improve your C#.Net code.

Programming
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 

» More on Programming
 



Share this article :
Click to see more related articles