Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么条件(三元)运算符看起来要快得多?_C#_Stopwatch - Fatal编程技术网

C# 为什么条件(三元)运算符看起来要快得多?

C# 为什么条件(三元)运算符看起来要快得多?,c#,stopwatch,C#,Stopwatch,编辑 如果我正确使用秒表,并将迭代次数增加两个数量级,我会得到 时间为22404ms 正常时间为21403ms 这些结果更接近我的预期,让我觉得世界上一切都好(如果不是我的代码) 三元/条件运算符实际上稍微慢一些 从下面开始,我有一部分 我在x64发布模式下编译了这个控制台应用程序,并启用了优化,然后在没有附加调试程序的情况下从命令行运行它 using System; using System.Diagnostics; class Program { static void Mai

编辑

如果我正确使用秒表,并将迭代次数增加两个数量级,我会得到

时间为22404ms

正常时间为21403ms

这些结果更接近我的预期,让我觉得世界上一切都好(如果不是我的代码)

三元/条件运算符实际上稍微慢一些


从下面开始,我有一部分

我在x64发布模式下编译了这个控制台应用程序,并启用了优化,然后在没有附加调试程序的情况下从命令行运行它

using System; 
using System.Diagnostics;

class Program
{
    static void Main()
    {
        var stopwatch = new Stopwatch();

        var ternary = Looper(10, Ternary);
        var normal = Looper(10, Normal);

        if (ternary != normal)            {
            throw new Exception();
        }

        stopwatch.Start();
        ternary = Looper(10000000, Ternary);
        stopWatch.Stop();
        Console.WriteLine(
            "Ternary took {0}ms", 
            stopwatch.ElapsedMilliseconds);

        stopwatch.Start();
        normal = Looper(10000000, Normal);
        stopWatch.Stop();
        Console.WriteLine(
            "Normal took {0}ms", 
            stopwatch.ElapsedMilliseconds);

        if (ternary != normal)            {
            throw new Exception();
        }

        Console.ReadKey();
    }

    static int Looper(int iterations, Func<bool, int, int> operation)
    {
        var result = 0;
        for (int i = 0; i < iterations; i++)
        {
            var condition = result % 11 == 4;
            var value = ((i * 11) / 3) % 5;
            result = operation(condition, value);
        }

        return result;
    }

    static int Ternary(bool condition, in value)
    {
        return value + (condition ? 2 : 1);
    }

    static int Normal(int iterations)
    {
        if (condition)
        {
            return = 2 + value;
        }
        
        return = 1 + value;
    }
}
虽然
三元
CIL稍微短一些,但在我看来,通过CIL的任何函数的执行路径都需要3次加载、1次或2次跳转和一次返回。为什么三元函数的速度似乎是原来的两倍


我认为,在实践中,这两种方法都很快,而且确实很快,但我想理解其中的差异。

这两种方法所用的时间几乎完全相同

您的结果已关闭,因为您根本没有正确使用秒表。“正常”的测量值包括两个活套所用的时间

如果你换第二个

stopwatch.Start();

然后您将得到正确的结果


顺便说一下,为了得到更公平的比较,您可能应该执行

    return (condition ? value + 2 : value + 1);
而不是

    return value + (condition ? 2 : 1);

所以它与另一个函数完全等价。否则,您不仅要测量条件运算符。

您不会在两次运行之间重置秒表。我从未亲自使用过该类,但文档表明
Start()
方法将恢复计时。我认为差异没有你想象的那么大。尝试将第二个
Start()?这在这里是怎么起作用的?@Roddyoffrozenpeas,你是对的。看看其他人的答案:回答我自己的问题,不同的方法并不重要。但是我们仍然可以理解在最初的问题中是如何衡量这一点的…@Henkholtman必须有一种一致的代码基准测试方法。没有这一点,你怎么可能希望优化呢?@RobbieDee-没有人说这很容易,有用的基准测试非常困难。
    return (condition ? value + 2 : value + 1);
    return value + (condition ? 2 : 1);