Detecting Negative Numbers

Benchmark created by Spencer Alan Watson on


Description

In this test we will do various ways of detecting if a number is negative or positive to see which is faster.

Setup

var n1 = -1,
    n2 = 0,
    n3 = 1,
    n4 = -1e16;
    n5 = 1e16;

Test runner

Ready to run.

Testing in
TestOps/sec
Less Than Operator
if(n1 < 0);
if(n2 < 0);
if(n3 < 0);
if(n4 < 0);
if(n5 < 0);
ready
Bit Shifting
if(n1 >> 31);
if(n2 >> 31);
if(n3 >> 31);
if(n4 >> 31);
if(n5 >> 31);
 
ready

Revisions

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

  • Revision 1: published by Spencer Alan Watson on