Closed GoogleCodeExporter closed 8 years ago
I've been curious about this, and I haven't been able to get a decisive
benchmark on it
either way. In traditional native code a multiply is almost always faster than
a
divide, so caching the inverse and multiplying is very commonplace. In select
circumstances in javascript, however, it seems that doing the divides directly
can
actually be faster. Can anyone provide me with a good explanation for this?
Original comment by Tojiro@gmail.com
on 5 Jun 2010 at 4:56
Looks like it is indeed slower, at least in my microbenchmark.
<html>
<script type="text/javascript">
var d = new Date();
var i = 10.4;
var x = 10.1;
var y = 15.1;
var z = 90.1;
for (var a = 0; a < 10000000; a++) {
var il = 1/i;
x*=il;
y*=il;
z*=il;
}
var e = new Date() - d;
var f = new Date();
var i = 10.4;
var x = 10.1;
var y = 15.1;
var z = 90.1;
for (var a = 0; a < 10000000; a++) {
x/=i;
y/=i;
z/=i;
}
var g = new Date() - f;
alert(e);
alert(g);
</script>
</html>
Original comment by danielhe...@gmail.com
on 5 Jun 2010 at 7:31
Original comment by Tojiro@gmail.com
on 12 Jun 2010 at 5:19
Running the benchmark you posted I'm getting ~1600ms for the *= version and
~1800ms for the /= version in Chrome, but ~120ms for *= and ~95 ms for /= in
Minefield, so we're at a bit of an impasse.
BTW: Has anyone else noticed that benchmark times ALWAYS seem extremely low on
minefield? I know they've been putting a lot of work into it, but 13 times
faster than chrome seems excessive. I think they may be recognizing that the
loop has no external effect and removing most of it! :(
In any case, IF the above benchmarks are true, I would prefer to optimize
against the browser that needs it most (in this case Chrome). So I believe that
for the time being I will be sticking with the multiply by inverse method.
Original comment by Tojiro@gmail.com
on 12 Jun 2010 at 5:46
Original issue reported on code.google.com by
danielhe...@gmail.com
on 5 Jun 2010 at 10:05