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

Revision 4 of this benchmark created by Hank Schultz 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.

Setup

var i = 10.0;
    var j = 10.0;
    var k = 10.0;

Test runner

Ready to run.

Testing in
TestOps/sec
Multiplication (int)
i = i * 2.0;
i = i * 4.0;
k = k * 65536.0;
ready
Bit-Shifting (left)
i = i << 1;
j = j << 2;
k = k << 16;
ready
Division Operator
i = i / 2.0;
j = j / 4.0;
k = k / 8.0;
ready
Division through Multiplying by a Float
i = i * 0.5;
j = j * 0.25;
k = k * 0.125;
ready
Division through bit-shifting right.
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.