Multiplication (int) vs Bit-Shifting (left) (v5)

Revision 5 of this benchmark created on


Description

A friend of mine was pretty excited to learn that there are possible speed increases associated with shifting bits over the multiplication operator, or by using multiplication over the division operator. However, I wonder if a good compiler or interpreter wouldn't make these optimization choices for you! Let's find out with JS!

This test is more effective than earlier versions of this test. On the other versions, the function calls and calls to console.log have significantly more overhead than simply doing the math. However, the values must be stored, as any reasonable smart optimizer will see that code not stored is irrelevant and therefore won't run it.

Test runner

Ready to run.

Testing in
TestOps/sec
Multiplication (int)
var i = 10.0;
var j = 10.0;
var k = 10.0;

i = i * 2.0;
i = i * 4.0;
k = k * 8.0;
ready
Bit-Shifting (left)
var i = 10.0;
var j = 10.0;
var k = 10.0;

i = i << 1;
j = j << 2;
k = k << 3;
ready
Division Operator
var i = 10.0;
var j = 10.0;
var k = 10.0;

i = i / 2.0;
j = j / 4.0;
k = k / 8.0;
ready
Division through Multiplying by a Float
var i = 10.0;
var j = 10.0;
var k = 10.0;

i = i * 0.5;
j = j * 0.25;
k = k * 0.125;
ready
Division through bit-shifting right.
var i = 10.0;
var j = 10.0;
var k = 10.0;

i = i >> 1;
j = j >> 2;
k = k >> 3;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.