How Come when I sampling profile a program and it actually runs faster than not profiling?

点点圈 提交于 2019-12-23 12:07:57

问题


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();
        }
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/7517024/how-come-when-i-sampling-profile-a-program-and-it-actually-runs-faster-than-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!