This is expected. V8 has a special optimization for "Math.floor of a division".
The only way of "fixing" this would be to remove that optimization, in which case the "/2" case would become about as slow as the "*.5" case. That's not what you want, is it? ;-) Background: All of this is due to JavaScript, where every number is a double. As you know, integers are faster to handle on the CPU, so JS engines try to detect cases where they can internally represent/manipulate numbers as integers without violating JS semantics. Essentially, in the "floor of div" case V8 decides that it can treat the numbers as integers, and further that division by constant 2 can be done by bit shift, so in optimized code it actually emits a bit shift instruction. In the "*.5" case, on the other hand, a regular floating-point multiplication is performed, and the resulting floating-point number is then rounded (well, floored) to integer, which is a bit slower. On Sun, Apr 7, 2013 at 7:06 PM, <[email protected]> wrote: > Multiply is naturally faster than the divide opcode. > This statement does not consider the speed difference of integer vs. double operations. It also doesn't consider the fact that JS engines, like compilers, can do various optimizations, so it's fairly hard to predict what machine instruction(s) will be emitted for a given snippet of JS code (also, this can change over time as engines implement further optimizations). > In this test Chrome is fast with the bit shift, almost as fast with > Math.floor(i/2), BUT much slower than Math.floor(i*.5). Something seems > wrong here. > How is it wrong to have a fast path? > However, it should be fast with the second .floor also, since *.5 is a > faster form of /2. > Nope, as explained above, there's another difference. > & is this best posted here or on Chromium? This list is appropriate. -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
