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
static double distance(double x1, double y1, double x2, double y2) { | |
double dx = x2 - x1; | |
double dy = y2 - y1; | |
return Math.sqrt((dx * dx) + (dy * dy)); | |
} | |
static double constant(double x1, double y1, double x2, double y2) { | |
return 0.0d; | |
} |
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
@Benchmark | |
public void testMethod(MyState state, Blackhole blackhole) { | |
int sum1 = state.a + state.b; | |
int sum2 = state.a + state.a + state.b + state.b; | |
blackhole.consume(sum1); | |
blackhole.consume(sum2); | |
} |
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
@State(Scope.Thread) | |
public static class MyState { | |
public int a = 1; | |
public int b = 2; | |
} | |
@Benchmark | |
public int testMethod(MyState state) { | |
int sum = state.a + state.b; | |
return sum; |
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
@Benchmark | |
public int testMethod() { | |
int sum = 3; | |
return sum; | |
} |
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
@Benchmark | |
public void testMethod(Blackhole blackhole) { | |
int a = 1; | |
int b = 2; | |
int sum = a + b; | |
blackhole.consume(sum); | |
} |
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
@Benchmark | |
public int testMethod() { | |
int a = 1; | |
int b = 2; | |
int sum = a + b; | |
return sum; | |
} |
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
@Benchmark | |
public void testMethod() { | |
int a = 1; | |
int b = 2; | |
int sum = a + b; | |
} |
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
REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on | |
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial | |
experiments, perform baseline and negative tests that provide experimental control, make sure | |
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts. | |
Do not assume the numbers tell you what you want them to tell. | |
Benchmark (listSize) Mode Cnt Score Error Units | |
MyBenchmark.atomicLong 1000000 avgt 5 0.026 ± 0.001 s/op | |
MyBenchmark.atomicLong 10000000 avgt 5 0.263 ± 0.003 s/op | |
MyBenchmark.atomicLong 100000000 avgt 5 2.504 ± 0.466 s/op |
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 com.avenuecode.snippet; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Level; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; |
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
java -jar target/benchmarks.jar |