官术网_书友最值得收藏!

Microbenchmarks

Microbenchmarking is used if we want to know which methods perform better than others. A naive approach would wrap the called function inside a block of a measureTimeMillis or measureNanoTime function. These functions just calculate the difference between start and stop timestamps:

public inline fun measureTimeMillis(block: () -> Unit) : Long {
val start = System.currentTimeMillis()
block()
return System.currentTimeMillis() - start
}

At a first glance, this approach gives us an idea about the performance of the code inside the block, but if we perform several experiments, it will be clear that this is the wrong approach. We've already touched on how the JVM executes and changes programming code to perform optimizations. When lines of code are executed, the JVM collects information about them. It will then try to optimize them and collect new information the next time it executes these same lines. Therefore, we have to take into account this factor and measure code after a warm-up to simulate real-world conditions.

To implement microbenchmarking correctly, you have to use the Java Microbenchmark Harness. 

主站蜘蛛池模板: 双牌县| 平定县| 郑州市| 南开区| 昔阳县| 五台县| 察哈| 榆林市| 东阿县| 洱源县| 彝良县| 东阿县| 宜兰市| 奉节县| 金湖县| 潼关县| 镶黄旗| 罗田县| 金塔县| 昌黎县| 常熟市| 德保县| 乌鲁木齐市| 余干县| 邛崃市| 河间市| 太原市| 肥东县| 当雄县| 宁化县| 大新县| 呼图壁县| 杭锦后旗| 边坝县| 靖安县| 宜黄县| 龙口市| 宾川县| 同江市| 庆云县| 玉树县|