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

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. 

主站蜘蛛池模板: 彰化市| 民县| 永德县| 黄平县| 和林格尔县| 广南县| 抚松县| 伊金霍洛旗| 宽城| 长寿区| 遂川县| 英德市| 英吉沙县| 双辽市| 石景山区| 重庆市| 雅安市| 衡水市| 台州市| 东方市| 射阳县| 莱芜市| 茂名市| 龙州县| 平凉市| 山东省| 湘潭县| 萨迦县| 罗甸县| 同仁县| 临沭县| 南安市| 宜黄县| 防城港市| 上林县| 镇坪县| 普定县| 天水市| 巩留县| 威信县| 潼南县|