Skip to content

Instantly share code, notes, and snippets.

@fjenett
Created March 31, 2010 07:07
Show Gist options
  • Save fjenett/350033 to your computer and use it in GitHub Desktop.
Save fjenett/350033 to your computer and use it in GitHub Desktop.
// benchmark to see if right-shift is faster than floor( / ).
// this is to quickly find which grid-element a location belongs to.
// test is in 1D only.
void draw ()
{
//setup
int grid = 3;
float gridP = pow(2,grid); // 16 >> 4 == 16 / 2^4
float rnd = random(1000);
int fInt=0, fInt2=0;
// using right-shifting
long ts = System.currentTimeMillis();
for (int i=0; i< 20000; i++)
fInt = ((int)rnd) >> grid;
float ts1 = (System.currentTimeMillis()-ts)/1000.0;
// using floor
ts = System.currentTimeMillis();
for (int i=0; i< 20000; i++)
fInt2 = floor(rnd / gridP);
float ts2 = (System.currentTimeMillis()-ts)/1000.0;
// results
println( rnd + " shift: " + fInt + " " + ts1 );
println( rnd + " floor: " + fInt2 + " " + ts2 );
println( "" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment