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

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. 

主站蜘蛛池模板: 平乡县| 岢岚县| 大田县| 什邡市| 武汉市| 和田县| 工布江达县| 禄丰县| 公主岭市| 马关县| 东宁县| 安庆市| 汉源县| 棋牌| 宜春市| 饶平县| 同德县| 武宁县| 钦州市| 钦州市| 台中县| 漠河县| 康乐县| 莒南县| 宜春市| 兴安盟| 庆云县| 松江区| 永登县| 宜川县| 阿坝县| 肥西县| 石楼县| 金沙县| 汶上县| 固镇县| 辛集市| 宝丰县| 承德县| 阳朔县| 乌海市|