Last active
August 29, 2015 14:21
-
-
Save amaembo/110c4499398d66880a73 to your computer and use it in GitHub Desktop.
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 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); | |
} | |
} |
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
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