4

I use DotTrace 4.5 performance

time in release mode:

 2400000000
 Basic: 00:00:08.8051103
 2400000000
 Five: 00:00:09.1561338
 2400000000
 Overload: 00:00:16.3740938
 2400000000
 IListtoFive: 00:00:15.5841445

time when profiling in release mode.

 2400000000
 Basic: 00:00:01.0048224
 2400000000
 Five: 00:00:03.5416982
 2400000000
 Overload: 00:00:11.8009959
 2400000000
 IListtoFive: 00:00:11.2568770

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace testLineIndex
{

    class Program
    {
        static long Five(int s0, int s1, int s2, int s3, int s4)
        {
            return s4 + 100 * s3 + 10000 * s2 + 1000000 * s1 + 100000000 * s0;
        }

        static long Overload(IList<int> line)
        {
            return Five(line[0], line[1], line[2], line[3], line[4]);
        }

        static long IListtoFive(IList<int> line)
        {
            return line[0]+100* line[1]+10000* line[2]+1000000* line[3]+100000000*line[4];
        }

        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();

            long testSize = 400000000;
            //List<int> myList = new List<int> { 1, 2, 3, 4, 5 };
            int[] myList = new int[] { 1, 2, 3, 4, 5 };
            long checksum = 0;
            watch.Start();
            for (long i = 0; i < testSize; i++)
            {
                long ret = Five(1,2,3,4,5);
                checksum += ret % 9;
            }
            watch.Stop();
            Console.WriteLine(checksum);
            Console.WriteLine("Basic: {0}", watch.Elapsed);

            checksum = 0;
            watch.Restart();
            for (long i = 0; i < testSize; i++)
            {
                long ret = Five(myList[0], myList[1], myList[2], myList[3], myList[4]);
                checksum += ret % 9;
            }
            watch.Stop();
            Console.WriteLine(checksum);
            Console.WriteLine("Five: {0}",watch.Elapsed);

            checksum = 0;
            watch.Restart();
            for (long i = 0; i < testSize; i++)
            {
                long ret = Overload(myList);
                checksum += ret % 9;
            }
            watch.Stop();
            Console.WriteLine(checksum);
            Console.WriteLine("Overload: {0}", watch.Elapsed);

            checksum = 0;
            watch.Restart();
            for (long i = 0; i < testSize; i++)
            {
                long ret = IListtoFive(myList);
                checksum += ret % 9;
            }
            watch.Stop();
            Console.WriteLine(checksum);
            Console.WriteLine("IListtoFive: {0}", watch.Elapsed);
            Console.ReadKey();
        }
    }
}
colinfang
  • 20,909
  • 19
  • 90
  • 173
  • Just a clarification- are you asking "Why does a program run faster in release mode than in debug mode?" – Chris Shain Sep 22 '11 at 15:21
  • 1
    I think I see now- you are asking why does profiling seem to speed up the application (vs just running it). Question- does your baseline number come while running it in VS or just running the compiled EXE? – Chris Shain Sep 22 '11 at 15:26
  • @ChrisShain Thx a lot, u hit the spot! I ran it in VS but the compiled exe... I checked again by running the exe without VS, it becomes faster. Though I have no idea why the speed would differ. Could u provide me with a detailed answer? I will mark that as the proper answer. – colinfang Sep 22 '11 at 15:30
  • Are you running the profiler soon after running it non-profiled? Usually executing code multiple times in a row leads to speed ups as optimizations are performed and things are cached. – Matt H Sep 22 '11 at 15:48
  • @colinfang: of course the speed differs when running in a debugger vs not running in a debugger. When you are running the code under the debugger the CLR is spending time communicating with the debugger, the garbage collector becomes less aggressive so that it does not clean up resources while you are potentially looking at them in the debugger, the jitter generates less efficient code so that it is easier to debug, and so on. You can't get any reliable performance data if a debugger is attached. – Eric Lippert Sep 22 '11 at 16:12
  • possible duplicate of [Launching VS Profiler boosts Application Performance x20?](http://stackoverflow.com/questions/5417663/launching-vs-profiler-boosts-application-performance-x20) – Mike Nakis May 09 '12 at 05:48

1 Answers1

6

My suspicion is the debugger. Even if you are running it in release mode, running it from VS attaches the debugger, which would slow it down considerably.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • 1
    +1. Impact of attaching debugger to release version can be decreased by unchecking Tools->Options->Debugging->"Suppress JIT Optimizatons..." – Alexei Levenkov Sep 22 '11 at 16:28