Test C# Code Performance Speed
Posted: Wednesday, June 25, 2008
by Armando Park
Visual C# Kicks
Programming in C#, as in many languages, provides a limitless way to write
applications. So many different programming techniques can vary in your
application's speed and efficiency. Learning how to determine which coding
techniques are faster is an essential skill.
The simple structure to test out fast C# code run is this:
An optional step is to divide the total time it took to execute the C# code and divided by the amount of times the for loop ran.I personally like to compare the total execution times since it reduces the variation in the results. Usually the first time the code is executed the source code will run slower than normal, so try to run several tests and average out the results.
Also I recommend to test source code from within Visual Studio (the debuggin environment in other words) since source code that runs with the debugger runs a bit slower. This gives a nicer window to compare different bits of source code against their speeds.
Remember that the final Release version of your application will be optimized by the compiler (inline code and such) and will almost always run faster than the debugging version.
If you would like general tips on optimizing C# code also visit the 5 Ways to Optimize C# Code
article.The simple structure to test out fast C# code run is this:
- Set up all variables required
- Set up a DateTime start variable to mark the beginning of the test
- Initialize a for loop with the code to be tested inside (the amount of trials should be set up to make the total execution time last an appropriate amount)
- Mark the ending time on a DateTime end variable
- Subtract the starting time to the ending time
- The difference can then be compared to other tests
An optional step is to divide the total time it took to execute the C# code and divided by the amount of times the for loop ran.I personally like to compare the total execution times since it reduces the variation in the results. Usually the first time the code is executed the source code will run slower than normal, so try to run several tests and average out the results.
Also I recommend to test source code from within Visual Studio (the debuggin environment in other words) since source code that runs with the debugger runs a bit slower. This gives a nicer window to compare different bits of source code against their speeds.
Remember that the final Release version of your application will be optimized by the compiler (inline code and such) and will almost always run faster than the debugging version.
This Article has been viewed 310 times. (Not updated in real-time.)
No comments yet.We want your comments! If you can read this, you don't have javascript enabled, so you can't use this comment system. Please enable javascript.