Multiplies faster than comparison in .NET?

Good morning day or night

Until today, I thought that comparison was one of the main instructions of the processor, and therefore it was one of the fastest operations that can be done on a computer ... On the other hand, I know that multiplication is sometimes more complicated and requires a lot of bit 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
Comparison: 9799.7

Quick explanation: j is a helper alternative variable that goes like (...), 11, -10, 9, -8, 7, (...) until it reaches zero, l is a variable that stores the sign of j , and a is a test variable that I want to always be equal to module j . The purpose of the test was to check whether it is faster to set this value of a using multiplication or a conditional operator.

Can anyone comment on these results?

Thank you very much.

+4
source share
4 answers

The second test is not just a comparison, but an if statement.

This is probably translated into a JUMP/BRANCH instruction on the CPU , including branch prediction (with possible pipeline blocks), and then probably slower than simple multiplication (even if not so many).

+5
source

It is often difficult to make such statements about the optimizing compiler. They do a lot of tricks that make simple cases different from real code. However, you are not just comparing, you are comparing / assigning in a very tight loop. The thread you are working on may pause several times in a branch; the multiplication can be assigned as many times as he likes, since the last assignment still has the last meaning, so many multiplications can begin immediately.

As a general rule, make your code clear and ignore minor time issues unless they become a problem. If you have a speed problem, a good trace / sync tool will help you much better than knowing if one operation is faster than another in a particular case.

+3
source

I assume that one comment I would make is that you do a lot more in the second operation:

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

Not only do you make a comparison, but also spectacularly "if..else .." with? operator and negation j.

0
source

You should try to run this test about 1000 times and use avrage to never compare with what the CLR does in the background.

0
source

Source: https://habr.com/ru/post/1343739/


All Articles