Created
March 6, 2014 23:07
-
-
Save buchgr/9401700 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package benchmark; | |
import org.openjdk.jmh.annotations.GenerateMicroBenchmark; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class NextEventLoopBenchmark { | |
@GenerateMicroBenchmark | |
public void newNext8 () { | |
MultiThreadedEventBlabla bla = new MultiThreadedEventBlabla(8); | |
bla.newNext10000(); | |
} | |
@GenerateMicroBenchmark | |
public void newNext9 () { | |
MultiThreadedEventBlabla bla = new MultiThreadedEventBlabla(9); | |
bla.newNext10000(); | |
} | |
@GenerateMicroBenchmark | |
public void newNext16 () { | |
MultiThreadedEventBlabla bla = new MultiThreadedEventBlabla(16); | |
bla.newNext10000(); | |
} | |
@GenerateMicroBenchmark | |
public void newNext19 () { | |
MultiThreadedEventBlabla bla = new MultiThreadedEventBlabla(19); | |
bla.newNext10000(); | |
} | |
@GenerateMicroBenchmark | |
public void existingNext16() { | |
MultiThreadedEventBlabla bla = new MultiThreadedEventBlabla(16); | |
bla.existingNext10000(); | |
} | |
@GenerateMicroBenchmark | |
public void existingNext19() { | |
MultiThreadedEventBlabla bla = new MultiThreadedEventBlabla(19); | |
bla.existingNext10000(); | |
} | |
private static class MultiThreadedEventBlabla { | |
private final AtomicInteger childIndex = new AtomicInteger(); | |
private final boolean poweroftwo; | |
private final int divisor; | |
public MultiThreadedEventBlabla(int childrenLength) { | |
poweroftwo = (childrenLength & (childrenLength-1)) == 0; | |
if (poweroftwo) | |
divisor = childrenLength-1; | |
else | |
divisor = childrenLength; | |
} | |
public void existingNext10000() { | |
int val = 0; | |
for (int i=0; i < 10000; i++) { | |
val = Math.abs(childIndex.getAndIncrement() % divisor); | |
} | |
bla(val); | |
} | |
public void newNext10000() { | |
int val = 0; | |
for (int i=0; i < 10000; i++) { | |
if (poweroftwo) { | |
val = childIndex.getAndIncrement() & divisor; | |
} else { | |
val = Math.abs(childIndex.getAndIncrement() % divisor); | |
} | |
} | |
bla(val); | |
} | |
private void bla(int val) { | |
if (val == -1) | |
System.out.println(val); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment