11

下面的代码正在检查执行相同解决方案的三种不同方法的性能。

    public static void Main(string[] args)
    {
        // for loop
        {
            Stopwatch sw = Stopwatch.StartNew();

            int accumulator = 0;
            for (int i = 1; i <= 100000000; ++i)
            {
                accumulator += i;
            }

            sw.Stop();

            Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, accumulator);
        }

        //Enumerable.Range
        {
            Stopwatch sw = Stopwatch.StartNew();

            var ret = Enumerable.Range(1, 100000000).Aggregate(0, (accumulator, n) => accumulator + n);

            sw.Stop();
            Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, ret);
        }

        //self-made IEnumerable<int>
        {
            Stopwatch sw = Stopwatch.StartNew();

            var ret = GetIntRange(1, 100000000).Aggregate(0, (accumulator, n) => accumulator + n);

            sw.Stop();
            Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, ret);
        }
    }

    private static IEnumerable<int> GetIntRange(int start, int count)
    {
        int end = start + count;

        for (int i = start; i < end; ++i)
        {
            yield return i;
        }
    }
}

结果是:

time = 306; result = 987459712
time = 1301; result = 987459712
time = 2860; result = 987459712

“for 循环”比其他两个解决方案更快并不奇怪,因为 Enumerable.Aggregate 需要更多的方法调用。然而,让我感到惊讶的是,“Enumerable.Range”比“自制的 IEnumerable”要快。我认为 Enumerable.Range 会比简单的 GetIntRange 方法有更多的开销。

可能的原因是什么?

4

4 回答 4

12

为什么Enumerable.Range要比你自制的慢GetIntRange?事实上,如果Enumerable.Range被定义为

public static class Enumerable {
    public static IEnumerable<int> Range(int start, int count) {
        var end = start + count;
        for(var current = start; current < end; ++current) {
            yield return current;
        }
    }
}

那么它应该和你自制的一样快GetIntRange。这实际上是 的参考实现Enumerable.Range,没有编译器或程序员的任何技巧。

您可能希望将您的GetIntRangeSystem.Linq.Enumerable.Range与以下实现进行比较(当然,正如 Rob 指出的那样,在发布模式下编译)。对于编译器从迭代器块生成的内容,此实现可能会稍微优化。

public static class Enumerable {
    public static IEnumerable<int> Range(int start, int count) {
        return new RangeEnumerable(start, count);
    }
    private class RangeEnumerable : IEnumerable<int> {
        private int _Start;
        private int _Count;
        public RangeEnumerable(int start, int count) {
            _Start = start;
            _Count = count;
        }
        public virtual IEnumerator<int> GetEnumerator() {
            return new RangeEnumerator(_Start, _Count);
        }
        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }
    }
    private class RangeEnumerator : IEnumerator<int> {
        private int _Current;
        private int _End;
        public RangeEnumerator(int start, int count) {
            _Current = start - 1;
            _End = start + count;
        }
        public virtual void Dispose() {
            _Current = _End;
        }
        public virtual void Reset() {
            throw new NotImplementedException();
        }
        public virtual bool MoveNext() {
            ++_Current;
            return _Current < _End;
        }
        public virtual int Current { get { return _Current; } }
        object IEnumerator.Current { get { return Current; } }
    }
}
于 2009-01-03T02:19:35.527 回答
5

我的猜测是您正在调试器中运行。这是我的结果,使用“/o+ /debug-”从命令行构建

time = 142; result = 987459712
time = 1590; result = 987459712
time = 1792; result = 987459712

还是有细微的差别,但不是很明显。迭代器块的实现不如量身定制的解决方案那么有效,但它们非常好。

于 2009-01-03T08:47:44.480 回答
4

假设这是一个正在运行的发布版本,否则所有比较都将关闭,因为 JIT 将无法正常工作。

您可以查看带有反射器的组件,并查看“产量”声明也正在扩展。编译器将创建一个类来封装迭代器。也许在生成的代码中进行的内务处理比 Enumerable.Range 的实现要多,这可能是手工编码的

于 2009-01-03T02:16:35.673 回答
2

反射器输出的细微差别(以及参数检查和额外的内部化水平在这里绝对不相关)。基本代码更像是:

public static IEnumerable<int> Range(int start, int count) {
    for(int current = 0; current < count; ++current) {
        yield return start + current;
    }
}

也就是说,它们不是另一个局部变量,而是为每个产量应用一个额外的加法。

我试图对此进行基准测试,但我无法停止足够多的外部流程来获得可以理解的结果。我还尝试了每个测试两次以忽略 JIT 编译器的影响,但即使这样也有“有趣”的结果。

这是我的结果示例:

运行 0:
时间 = 4149; 结果 = 405000000450000000
时间 = 25645; 结果 = 405000000450000000
时间 = 39229; 结果 = 405000000450000000
时间 = 29872; 结果 = 405000000450000000

时间 = 4277; 结果 = 405000000450000000
时间 = 26878; 结果 = 405000000450000000
时间 = 26333; 结果 = 405000000450000000
时间 = 26684; 结果 = 405000000450000000

运行 1:
时间 = 4063; 结果 = 405000000450000000
时间 = 22714; 结果 = 405000000450000000
时间 = 34744; 结果 = 405000000450000000
时间 = 26954; 结果 = 405000000450000000

时间 = 4033; 结果 = 405000000450000000
时间 = 26657; 结果 = 405000000450000000
时间 = 25855;结果 = 405000000450000000
时间 = 25031; 结果 = 405000000450000000

运行 2:
时间 = 4021; 结果 = 405000000450000000
时间 = 21815; 结果 = 405000000450000000
时间 = 34304; 结果 = 405000000450000000
时间 = 32040; 结果 = 405000000450000000

时间 = 3993; 结果 = 405000000450000000
时间 = 24779; 结果 = 405000000450000000
时间 = 29275; 结果 = 405000000450000000
时间 = 32254; 结果 = 405000000450000000

和代码

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

namespace RangeTests
{
  class TestRange
  {
    public static void Main(string[] args)
    {
      for(int l = 1; l <= 2; ++l)
      {
        const int N = 900000000;
        System.GC.Collect(2);
        // for loop
        {
            Stopwatch sw = Stopwatch.StartNew();

            long accumulator = 0;
            for (int i = 1; i <= N; ++i)
            {
                accumulator += i;
            }

            sw.Stop();

            Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, accumulator);
        }
        System.GC.Collect(2);

        //Enumerable.Range
        {
            Stopwatch sw = Stopwatch.StartNew();

            var ret = Enumerable.Range(1, N).Aggregate(0, (long accumulator,int n) => accumulator + n);

            sw.Stop();
            Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, ret);
        }
        System.GC.Collect(2);

        //self-made IEnumerable<int>
        {
            Stopwatch sw = Stopwatch.StartNew();

            var ret = GetIntRange(1, N).Aggregate(0, (long accumulator,int n) => accumulator + n);

            sw.Stop();
            Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, ret);
        }
        System.GC.Collect(2);

        //self-made adjusted IEnumerable<int>
        {
            Stopwatch sw = Stopwatch.StartNew();

            var ret = GetRange(1, N).Aggregate(0, (long accumulator,int n) => accumulator + n);

            sw.Stop();
            Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, ret);
        }
        System.GC.Collect(2);
        Console.WriteLine();
    } }

    private static IEnumerable<int> GetIntRange(int start, int count)
    {
        int end = start + count;

        for (int i = start; i < end; ++i)
        {
            yield return i;
        }
    }

    private static IEnumerable<int> GetRange(int start, int count)
    {
        for (int i = 0; i < count; ++i)
        {
            yield return start + i;
        }
    }
} }

编译

csc.exe -optimize+ -debug- RangeTests.cs
于 2010-05-28T22:48:04.937 回答