Skip to content

Instantly share code, notes, and snippets.

@amaembo
Last active Aug 29, 2015
Embed
What would you like to do?
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