本文介绍了当我对程序进行采样分析时,它实际上比不进行分析时运行得更快,这是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DotTrace 4.5 性能

I use DotTrace 4.5 performance

发布模式时间:

 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

我的代码:

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

推荐答案

我怀疑是调试器.即使您在发布模式下运行它,从 VS 运行它也会附加调试器,这会大大减慢它的速度.

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.

这篇关于当我对程序进行采样分析时,它实际上比不进行分析时运行得更快,这是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:23