如何使用JMH计算CPU时间和内存量?例如,我有:代码:
@State(Scope.Thread)
@BenchmarkMode(Mode.All)
public class JMHSample_My {
int x = 1;
int y = 2;
@GenerateMicroBenchmark
public int measureAdd() {
return (x + y);
}
@GenerateMicroBenchmark
public int measureMul() {
return (x * y);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(".*" + JMHSample_My.class.getSimpleName() + ".*")
.warmupIterations(5)
.measurementIterations(5)
.forks(1)
.build();
new Runner(opt).run();
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
Benchmark Mode Samples Mean Mean error Units
JMHSample_My.measureAdd thrpt 5 1060579.757 39506.950 ops/ms
JMHSample_My.measureMul thrpt 5 1046872.684 79805.116 …Run Code Online (Sandbox Code Playgroud)