C# Code size and Code execution time

c#, performance

Solution

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;

Problem

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 ```

Original source