- Mastering High Performance with Kotlin
- Igor Kucherenko
- 316字
- 2021-06-25 20:55:25
Benchmark state
Our testMethod() function is a simple example, but most of the time our method with a critical piece of code depends on a state. State is a set of variables on which our code depends, but we don't want to initialize them in a benchmark method, and we need to maintain this state while the benchmark is running. Since the JMH is heavily used to build benchmarks with concurrent code, the concept of state-bearing objects was implemented. To implement the state class, you need to follow these rules:
- The class must be declared as public
- You can't use inner classes, but you can use nested classes that are declared with the static modifier
- The class must have a public constructor without arguments
A state class contains state variables, and if you specify an instance of this class as an argument it will be provided as a parameter to the @Benchmark method. In code, it might look as follows:
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
public class MyBenchmark {
@State(Scope.Thread)
public static class MyState {
int a = 3;
int b = 4;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void testMethod(MyState myState) {
int c = myState.a + myState.b;
}
}
We use a state in this example because the testMethod uses two constant values and the compiler can optimize this moment and just use the final result instead of calculations. If you look at the output, you'll see that the result time has doubled:
Result "org.sample.MyBenchmark.testMethod":
0.671 ±(99.9%) 0.071 ns/op [Average]
(min, avg, max) = (0.656, 0.671, 0.702), stdev = 0.018
CI (99.9%): [0.600, 0.743] (assumes normal distribution)
# Run complete. Total time: 00:00:10
Benchmark Mode Cnt Score Error Units
MyBenchmark.testMethod avgt 5 0.671 ± 0.071 ns/op
The @State annotation takes an instance of the Scope enum, which defines how to share the instance of the MyState class between threads.
- C++程序設(shè)計(jì)(第3版)
- ASP.NET MVC4框架揭秘
- Xamarin.Forms Projects
- 程序設(shè)計(jì)基礎(chǔ)教程:C語(yǔ)言
- RSpec Essentials
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)案例教程
- Unity 2018 Shaders and Effects Cookbook
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- IoT Projects with Bluetooth Low Energy
- ASP.NET 4.0 Web程序設(shè)計(jì)
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- C Primer Plus(第6版)中文版【最新修訂版】
- 零基礎(chǔ)學(xué)編程系列(全5冊(cè))
- 少兒編程輕松學(xué)(全2冊(cè))
- Learning ECMAScript 6