为什么条件(三元)运算符似乎显著快? [英] Why does the conditional (ternary) operator seem significantly faster?

查看:150
本文介绍了为什么条件(三元)运算符似乎显著快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

如果我用秒表正确和最多的迭代次数由两个数量级,我得到

If I use Stopwatch correctly and up the number of iterations by two orders of magnitude I get

三元了22404ms

Ternary took 22404ms

普通了21403ms

Normal took 21403ms

这些结果更接近我所期待的,让我觉得一切是正确的与世界(如果不是我的代码。)

These results are closer to what I was expecting and make me feel all is right with the world (if not with my code.)

中的三元/有条件的经营者,其实是稍微慢一些。

The Ternary/Conditional operator is in fact marginally slower.

通过继这个问题,我有一部分的回答

我编译的x64释放模式这个控制台应用程序,与优化,并在命令行没有运行。连接调试

I compile this console app in x64 Release Mode, with optimizations on, and run it from the command line without a debugger attached.

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



我没有得到任何的异常,并输出到控制台是财产以后接近,

I don't get any exceptions and the output to the console is somthing close to,

三元了107ms

普通了230ms

当我打破CIL两个逻辑功能,我得到这个,

When I break down the CIL for the two logical functions I get this,

... Ternary ...
{
     : ldarg.1      // push second arg
     : ldarg.0      // push first arg
     : brtrue.s T   // if first arg is true jump to T
     : ldc.i4.1     // push int32(1)
     : br.s F       // jump to F
    T: ldc.i4.2     // push int32(2)
    F: add          // add either 1 or 2 to second arg
     : ret          // return result
}

... Normal ...
{
     : ldarg.0      // push first arg
     : brfalse.s F  // if first arg is false jump to F
     : ldc.i4.2     // push int32(2)
     : ldarg.1      // push second arg
     : add          // add second arg to 2
     : ret          // return result
    F: ldc.i4.1     // push int32(1)
     : ldarg.1      // push second arg
     : add          // add second arg to 1
     : ret          // return result
}

虽然三元 CIL是有点短,在我看来,通过CIL的两个函数的执行路径需要3负载和1或2个跳跃和回报。为何三元功能似乎是快两倍。

Whilst the Ternary CIL is a little shorter, it seems to me that the execution path through the CIL for either function takes 3 loads and 1 or 2 jumps and a return. Why does the Ternary function appear to be twice as fast.

我underdtand的是,在实践中,他们都非常快,事实上,基切不够好,但我想了解的差异。

I underdtand that, in practice, they are both very quick and indeed, quich enough but, I would like to understand the discrepancy.

推荐答案

这两个带几乎一模一样长的时间。

The two take pretty much exactly the same amount of time.

您的结果了,因为你根本没有使用秒表正确。对于正常的测量包括由所花费的时间的两个的活套。

Your results are off because you simply didn’t use Stopwatch correctly. The measurement for "Normal" includes the time taken by both loopers.

如果你改变了第二个

stopwatch.Start();



to

stopwatch.Restart();

然后你会得到正确的结果。

Then you will get the correct results.

顺便说一句,得到一个更公平的比较,你应该执行

By the way, to get a fairer comparison, you should probably execute

    return (condition ? value + 2 : value + 1);



而不是

instead of

    return value + (condition ? 2 : 1);



,以便它是完全等同于其他功能。否则,你测量的不仅的条件操作。

这篇关于为什么条件(三元)运算符似乎显著快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆