Created
April 1, 2013 20:20
-
-
Save eleco/5287428 to your computer and use it in GitHub Desktop.
bitwise ops benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.carrotsearch.junitbenchmarks.BenchmarkOptions; | |
import com.carrotsearch.junitbenchmarks.BenchmarkRule; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertTrue; | |
public class DivideBenchmark { | |
final static int reps = 1000 * 1000 *10 ; | |
int divide[] = new int[reps]; | |
@Before | |
public void setup(){ | |
for (int i =0; i<reps; i++) | |
divide[i]= i / 64; | |
} | |
@Rule | |
public BenchmarkRule benchmarkRun = new BenchmarkRule(); | |
@Test | |
@BenchmarkOptions(benchmarkRounds =9, warmupRounds = 1, concurrency = 1) | |
public void testJVMDivision() throws Exception { | |
for (int i=0; i<reps;i++) | |
assertTrue(divide[i]==i /64); | |
} | |
@Test | |
@BenchmarkOptions(benchmarkRounds = 9, warmupRounds = 1, concurrency = 1) | |
public void testBitwiseDivision() throws Exception { | |
for (int i=0; i<reps;i++) | |
assertTrue(divide[i]==(i >> 6)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.carrotsearch.junitbenchmarks.BenchmarkOptions; | |
import com.carrotsearch.junitbenchmarks.BenchmarkRule; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertTrue; | |
public class ModuloBenchmark { | |
final static int reps = 1000 * 1000 *50; | |
final static int modulos[] = new int[reps]; | |
@Before | |
public void setup(){ | |
for (int i =0; i<reps; i++) | |
modulos[i]= i % 1024; | |
} | |
@Rule | |
public BenchmarkRule benchmarkRun = new BenchmarkRule(); | |
@Test | |
@BenchmarkOptions(benchmarkRounds =9, warmupRounds = 1, concurrency = 1) | |
public void testJVMModulo() throws Exception { | |
for (int i=0; i<reps;i++) | |
assertTrue(modulos[i]==i % 1024); | |
} | |
@Test | |
@BenchmarkOptions(benchmarkRounds = 9, warmupRounds = 1, concurrency = 1) | |
public void testBitwiseModulo() throws Exception { | |
for (int i=0; i<reps;i++) | |
assertTrue(modulos[i]==(i &1023)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.carrotsearch.junitbenchmarks.BenchmarkOptions; | |
import com.carrotsearch.junitbenchmarks.BenchmarkRule; | |
import org.junit.BeforeClass; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertTrue; | |
public class MultBenchmark { | |
static final int reps = 1000 * 1000 * 10 ; | |
static final int mult[] = new int[reps]; | |
@BeforeClass | |
public static void setup(){ | |
for (int i =0; i<reps; i++) | |
mult[i]= i * 64; | |
} | |
@Rule | |
public BenchmarkRule benchmarkRun = new BenchmarkRule(); | |
@Test | |
@BenchmarkOptions(benchmarkRounds = 9, warmupRounds = 1, concurrency = 1) | |
public void testJVMMultiply() throws Exception { | |
for (int i=0; i<reps;i++) | |
assertTrue(mult[i]==i *64); | |
} | |
@Test | |
@BenchmarkOptions(benchmarkRounds = 9, warmupRounds = 1, concurrency = 1) | |
public void testBitwiseMultiply() throws Exception { | |
for (int i=0; i<reps;i++) | |
assertTrue(mult[i]==(i << 6)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.carrotsearch.junitbenchmarks.BenchmarkOptions; | |
import com.carrotsearch.junitbenchmarks.BenchmarkRule; | |
import org.junit.BeforeClass; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TestRule; | |
import static junit.framework.Assert.assertTrue; | |
public class NextPowerOfTwoBenchmark { | |
static final int reps = 1000 * 1000 * 10 ; | |
static final int nxtPower[] = new int[reps]; | |
@BeforeClass | |
public static void setup(){ | |
for (int i =2; i<reps; i++) { | |
nxtPower[i] = (int)Math.pow(2,Math.ceil(Math.log(i) / Math.log(2))); | |
} | |
} | |
@Rule | |
public TestRule benchmarkRun = new BenchmarkRule(); | |
@Test | |
@BenchmarkOptions(benchmarkRounds =9, warmupRounds = 1,callgc = false,concurrency = 1) | |
public void testJVMNextPowerOfTwo() throws Exception { | |
for (int i=2; i<reps;i++) | |
assertTrue(nxtPower[i]==(int)Math.pow(2,Math.ceil(Math.log(i)/Math.log(2)))); | |
} | |
@Test | |
@BenchmarkOptions(benchmarkRounds = 9, warmupRounds = 1, callgc = false,concurrency = 1) | |
public void testBitwisePowerOfTwo() throws Exception { | |
for (int i=2; i<reps;i++) | |
assertTrue(nxtPower[i]==( 1 << (32 - Integer.numberOfLeadingZeros(i - 1)))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment