Reputation: 7043
Is there any way to find what is a size of a code and what is its execution time so I can compare two codes and decide which is better?
For example lets say I want to find the size and execution time of this
Code 1
for(int i=0; i<5; i++)
{
sum+=1;
}
and this
Code 2
for(int i=0; i<=4; i++)
{
sum = sum + 1;
}
to decide which is better (I don't care about this example now). For example the result will be:
Code 1:
Size: ? KB
Time: ? ms
Code 2:
Size: ? KB
Time: ? ms
Upvotes: 7
Views: 5115
Reputation: 499002
To measure execution time you should use StopWatch
- you will need to run multiple iterations multiple times and average them out if you want a proper benchmark.
var sw = new StopWatch();
sw.Start();
// do a million iterations
sw.Stop();
var time = sw.Elapsed;
As for in memory sizes - you can use one of the many memory profilers available - ANTS memory profiler, dotTrace are two commercial options.
Upvotes: 3
Reputation: 1742
You could either use ANTS Profiler http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/ (payable product but they have a Trial version) or some other Profiler product (ANTS, vTune, OptimizeIt, DevPartner, YourKit, dotTrace) on the market.
You may also intstrument the functions by yourself by setting up some unit tests that execute those 2 functions using a manual StopWatch instrumentation to compare execution time (quicker and less expensive). Unit tests would also allow for assuring that you do not have any regressions on performance if you need to change the implementation later. http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
var stopWatch = new Stopwatch();
stopWatch.Start();
var result = CallFunction();
stopWatch.Stop();
var executionTime = stopWatch.Elapsed;
Upvotes: 15