Skip to content

Instantly share code, notes, and snippets.

@amaembo
Last active August 29, 2015 14:21
Show Gist options
  • Save amaembo/110c4499398d66880a73 to your computer and use it in GitHub Desktop.
Save amaembo/110c4499398d66880a73 to your computer and use it in GitHub Desktop.
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.annotations.*;
@Warmup(iterations = 5, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 3, timeUnit = TimeUnit.SECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(2)
@State(Scope.Benchmark)
public class AddTest {
long add(long sum) {
int hash = hashCode();
BigDecimal number = new BigDecimal(hash);
sum += number.longValue();
return sum;
}
long add1(long sum) {
BigDecimal number = new BigDecimal(hashCode());
sum += number.longValue();
return sum;
}
@Benchmark
public void testAdd(Blackhole bh) {
long count = 100000000l;
long sum = 0;
for (long i=0; i<count; i++) {
sum = add(sum);
}
bh.consume(sum);
}
@Benchmark
public void testAdd1(Blackhole bh) {
long count = 100000000l;
long sum = 0;
for (long i=0; i<count; i++) {
sum = add1(sum);
}
bh.consume(sum);
}
}
Benchmark Mode Cnt Score Error Units
AddTest.testAdd avgt 20 214.740 ± 4.323 ms/op
AddTest.testAdd1 avgt 20 1138.269 ± 32.062 ms/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment