简体   繁体   English

乘法比.NET中的比较更快?

[英]Is Multiplication Faster Than Comparison in .NET?

Good morning, afternoon or night, 早上好,下午或晚上,

Up until today, I thought comparison was one of the basic processor instructions, and so that it was one of the fastest operations one can do in a computer... On the other hand I know multiplication in sometimes trickier and involves lots of bits operations. 直到今天,我认为比较是基本的处理器指令之一,因此它是计算机中可以做的最快的操作之一...另一方面,我知道乘法有时比较棘手并涉及大量的位操作。 However, I was a little shocked to look at the results of the following code: 但是,看到以下代码的结果我有点震惊:

Stopwatch Test = new Stopwatch();

int     a = 0;
int     i = 0, j = 0, l = 0;
double  c = 0, d = 0;

for (i = 0; i < 32; i++)
{
    Test.Start();

    for (j = Int32.MaxValue, l = 1; j != 0; j = -j + ((j < 0) ? -1 : 1), l = -l)
    {
        a = l * j;
    }

    Test.Stop();

    Console.WriteLine("Product: {0}", Test.Elapsed.TotalMilliseconds);
    c += Test.Elapsed.TotalMilliseconds;

    Test.Reset();
    Test.Start();

    for (j = Int32.MaxValue, l = 1; j != 0; j = -j + ((j < 0) ? -1 : 1), l = -l)
    {
        a = (j < 0) ? -j : j;
    }

    Test.Stop();

    Console.WriteLine("Comparison: {0}", Test.Elapsed.TotalMilliseconds);
    d += Test.Elapsed.TotalMilliseconds;

    Test.Reset();
    }

    Console.WriteLine("Product: {0}", c / 32);
    Console.WriteLine("Comparison: {0}", d / 32);
    Console.ReadKey();
}

Result: 结果:

Product: 8558.6 产品: 8558.6
Comparison: 9799.7 比较: 9799.7

Quick explanation : j is an ancillary alternate variable which goes like (...), 11, -10, 9, -8, 7, (...) until it reaches zero, l is a variable which stores j 's sign, and a is the test variable, which I want always to be equal to the modulus of j . 快速解释j是一个辅助的替代变量,它像(...), 11, -10, 9, -8, 7, (...)直到它达到零, l是一个存储j的符号的变量, a是测试变量,我希望它总是等于j的模数。 The goal of the test was to check whether it is faster to set a to this value using multiplication or the conditional operator. 测试的目的是检查使用乘法或条件运算符将a设置为此值是否更快。

Can anyone please comment on these results? 有人可以对这些结果发表评论吗?

Thank you very much. 非常感谢你。

Your second test it's not a mere comparison, but an if statement. 你的第二次测试不仅仅是比较,而是if语句。

That's probably translated in a JUMP/BRANCH instruction in CPU , involving branch prediction (with possible blocks of the pipeline) and then is likely slower than a simple multiplication (even if not so much). 这可能是在CPU中的JUMP/BRANCH指令中转换的,涉及分支预测 (可能有管道的块),然后可能比简单的乘法慢(即使不是那么多)。

It can often be very difficult to make such assertions about an optimising compiler. 通常很难对优化编译器做出这样的断言。 They do lots of tricks that make simple cases different from real code. 他们做了许多技巧,使简单的案例与实际代码不同。 That said, you aren't just doing a comparison, you're doing a compare/assign in a very tight loop. 也就是说,你不仅仅是在进行比较,而是在一个非常紧密的循环中进行比较/分配。 The thread you're working on may have to pause many times at the branch; 你正在处理的线程可能不得不在分支处暂停多次; the multiplication can assign as many times as it likes, as long as the last assignment is still last, so many multiplies can be going on at once. 乘法可以分配任意次数,只要最后一次分配仍然是最后一次,那么可以立即进行多次乘法。

As is the general rule, make your code clear and ignore minor timing issues unless they become a problem. 按照一般规则,使代码清晰,忽略小的时间问题,除非它们成为问题。 If you do have a speed problem a good tracing/timing tool will guide you much better than knowing if one operation is faster than other in a specific case. 如果确实存在速度问题,那么良好的跟踪/计时工具将比在特定情况下知道一个操作是否比其他操作更快更好地指导您。

I guess one comment I would make is that you are doing a lot more in the second operation: 我想我会做的一个评论是你在第二次操作中做了很多事情:

a = (j < 0) ? -j : j; 

Not only are you doing a comparison, but also effectivly a "if..else.." with the ? 你不仅要进行比较,还要有效地使用“if..else ..”。 operator and a negation of j. 运算符和j的否定。

您应该尝试运行此测试大约1000次并使用avrage来比较您现在从未在后台进行的CLR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM