Why is the comparison twice as fast as uint64?

I analyzed the following program using the Matlab profile . Both double and uint64 are 64-bit variables. Why is comparing two doubles so much faster than comparing two uint64? Aren't they compared bitwise?

 big = 1000000; a = uint64(randi(100,big,1)); b = uint64(randi(100,big,1)); c = uint64(zeros(big,1)); tic; for i=1:big if a(i) == b(i) c(i) = c(i) + 1; end end toc; a = randi(100,big,1); b = randi(100,big,1); c = zeros(big,1); tic; for i=1:big if a(i) == b(i) c(i) = c(i) + 1; end end toc; 

This is a profile measurement:

profile screenshot

This is what tictoc measures:

 Elapsed time is 6.259040 seconds. Elapsed time is 0.015387 seconds. 

The effect disappears when uint8..uint32 or int8..int32 are used instead of 64-bit data types.

+7
source share
1 answer

This is probably a combination of the Matlab interpreter and the main processor, which does not support 64-bit int types, as well as others.

Matlab stands for double over int operations. Most values ​​are stored in double types, even if they are integer values. The double and int == operations will use different code paths, and MathWorks will pay much more attention to tweaking and optimizing the code for double than for int, especially int64 . In fact, older versions of Matlab did not support arithmetic operations on int64 at int64 . (And IIRC, it still doesn't support mixed integer math.) When you do int64 , you use less mature, less tuned code, and the same goes for == . Int types are not a priority in Matlab. Having int64 may even interfere with the JIT optimization of this code, but this is just an assumption.

But there may be a basic hardware reason for this. Here's the hypothesis: if you are on a 32-bit x86, you are working with 32-bit general-purpose registries (integer). This means that smaller int types can fit into the register and compare using quick instructions, but 64-bit int values ​​will not fit into the register and can take more expensive sequences of commands for comparison. double s, even if they are 64 bits wide, will fit in wide floating-point registers of the x87 floating-point block and can be compared on hardware using quick floating-point comparison commands. This means that [u]int64 are the only ones that cannot be compared using fast single-mode operations with a single instruction.

If this happens, if you run the same code on a 64-bit version of x86-64 (on a 64-bit Matlab), the difference may disappear, because then you have 64-bit universal registers. But then again, this may not be the case if the Matlab interpreter code is not compiled to use it.

+6
source

All Articles