1. Don’t use Debug configuration
This is one common problems that developers do. They forget to switch configuration to Release and therefore they are measuring the assemblies that are full of debug symbols and debugger interactions.Code compiled without debug symbols and other debugger stuff works sometimes hundreds of times faster than Debug version of same code. On live environments you must use code compiled with Release configuration. There is no point to measure Debug code because it brings too much noise and you have no idea how debugger and other tools affect the measuring process.
2. Use Stopwatch instead of DateTime.Now
Measuring the speed of code is often done using DateTime.Now values before and after measuring. This is wrong because resulting time contains also time delays when current thread was suspended by operating system by example.If you want better results then use Stopwatch class. This class is perfect fit for scenario like this. Here is the example.
static void MeasureCalculation()
{
var watch = new Stopwatch();
watch.Start();
// do your calculations here
watch.Stop();
Console.WriteLine("Time: " + watch.Elapsed);
}
3. Measure only the code you need to measure
To get better results you have to measure only the code you need to measure and nothing else. This is not very uncommon way how some developers write their measuring code.static void MeasureCalculation()
{
var watch = new Stopwatch();
watch.Start();
var inputList = GetInputData();
RandomizeInputs(inputList);
// do your calculations here
inputList.Clear();
watch.Stop();
Console.WriteLine("Time: " + watch.Elapsed);
}
static void MeasureCalculation()
{
var inputList = GetInputData();
RandomizeInputs(inputList);
var watch = new Stopwatch();
watch.Start();
// do your calculations here
watch.Stop();
inputList.Clear();
Console.WriteLine("Time: " + watch.Elapsed);
}
4. Turn off diagnostic messages while measuring
Before measuring we need to make sure that our code contains only the functionalities that we need to measure. Still developers forget different diagnostic messages there and we cannot be sure in measuring results anymore. Take a look at the following code.static void MeasureCalculation()
{
var inputList = GetInputData();
RandomizeInputs(inputList);
var watch = new Stopwatch();
watch.Start();
// Start calculation
Debug.WriteLine("Initializing data for algorithm");
// Some more line
Logger.WriteLine("X(Y, Z)=" + z);
watch.Stop();
inputList.Clear();
Console.WriteLine("Time: " + watch.Elapsed);
}
static void MeasureCalculation()
{
var inputList = GetInputData();
RandomizeInputs(inputList);
var watch = new Stopwatch();
watch.Start();
// Start calculation
watch.Stop();
inputList.Clear();
Console.WriteLine("Time: " + watch.Elapsed);
}
5. Measure more than once and calculate averages
I think you have seen something like this in office or in internet:Q: “How long it took to run?”
A: “On my machine it takes about 1.5 seconds to run”
Q: “1.5 seconds… Is it average or just one run?”
A: “Just hit F5 and read the result, that’s it”
Well, my question is – what other processes were running on same machine that possibly may affect the results? Okay, there are a lot of processes. Instead of using one measuring round we need to make more runs and take their average. Here is the example.
static void MeasureCalculation()
{
var inputList = GetInputData();
RandomizeInputs(inputList);
var watch = new Stopwatch();
var cycles = Math.Pow(10, 6);
var times = 0D;
for (var i = 0; i < cycles; i++)
{
watch.Reset();
watch.Start();
// Call calculation
watch.Stop();
times += watch.ElapsedMilliseconds;
}
inputList.Clear();
Console.WriteLine("Time: " + (times / cycles));
}
Conclusion
Measuring the speed of code is not complex task. There are common mistakes that I listed here and if you avoid these mistakes you get way better and accurate measuring results. It is also possible to use testing frameworks but I focused here on quick and simple measuring method so you get your code measured right there where it is. You don’t have to install any additional tools or spend your time setting up more complex measuring environment. As you saw – measuring the speed of code is not rocket science.http://weblogs.asp.net/gunnarpeipman/archive/2010/09/09/
No comments:
Post a Comment