Why a cube is faster than a square

I wrote this:

var max = 0xffffff * 4; var step = 1 / max; function cube() { var result = 0.; for (var x = 0.; x < 1; x += step) { result += x * x * x; } return result; } function mul() { var result = 0.; for (var x = 0.; x < 1; x += step) { result += x * x; } return result; } function go() { var r = ''; r += cube() + ' \n'; r += mul() + ' \n'; alert(r); } 

and see the result in the Chrome profiler:

 mul: 106ms cube: 87ms 

How is this possible?

+42
performance javascript
Mar 23 '16 at 12:21
source share
4 answers

Your statement is incorrect. a cube is no faster than a mul, and your example does not prove it.

In fact, what happens is that the internals of Javascript take longer than the actual multiplication, which leads to very similar times for mul and cube. I ran two functions in a loop, just to increase the difference, and the profiler shows 20219 versus 20197, which is insignificant. And BTW, the cube here is "slower."

In addition, this profiling method does not work because both Chrome and Firefox optimize a lot before doing the math inside loops. What you think is a loop, it can very well use a cached value, or even a mathematical function that optimization knows, returns the same result.

Here is the code I used:

 <script> var max = 0xffffff * 4; var step = 1 / max; function cube() { var result = 0.; for (var x = 0.; x < 1; x += step) { result += x * x * x; } return result; } function mul() { var result = 0.; for (var x = 0.; x < 1; x += step) { result += x * x; } return result; } function go() { var s=''; for (var i=0; i<100; i++) { s+=cube(); s+=mul(); } console.log(s); } go(); </script> 

Also, as a reference, watch the video here: https://fosdem.org/2016/schedule/event/mozilla_benchmarking_javascript_tips/ , where a guy from Firefox explains why microdetection really doesn't mean much.

+33
Mar 29 '16 at 8:12
source share

This is probably because, since all your numbers are below 1, the cube function adds smaller numbers than the square, and (I'm not sure if this actually works), so it takes less time. This is just a hunch. And since the numbers are so small that it can also be due to inadequate accuracy. In addition, I tested with numbers more than one cube with them slower.

0
Apr 03 '16 at 0:29
source share

perhaps the optimizer decides that one of them can be executed with vector instructions, while the other uses the plain old fmul. I assume that "square" uses fmul and cube uses the mulpd vector instruction, which can multiply up to 4 doubles in one instruction. I added a “quad” that did 4 multiplications, and its time is pretty close to the cube. but when I went to the "five", it slowed down more slowly than a square. This is indirect evidence that vector instructions are used for the cube and ATV.

It would be interesting to see the results on an Intel processor with a processor on the tablet.

0
Apr 22 '16 at 1:59
source share

In some browsers, javascript runs as interpreted, while JIT compiles it in the background. After compiling javascript, it starts to work faster.

-5
Mar 25 '16 at 12:38 on
source share



All Articles