- Mastering High Performance with Kotlin
- Igor Kucherenko
- 178字
- 2021-06-25 20:55:24
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.
- 大學計算機基礎(第二版)
- 深入理解Android(卷I)
- C語言程序設計教程
- Mastering RabbitMQ
- HTML5 移動Web開發從入門到精通(微課精編版)
- Django:Web Development with Python
- C/C++常用算法手冊(第3版)
- 青少年學Python(第1冊)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(1)
- C語言程序設計上機指導與習題解答(第2版)
- MySQL從入門到精通(軟件開發視頻大講堂)
- RealSenseTM互動開發實戰
- TMS320LF240x芯片原理、設計及應用
- SQL Server 2008 R2數據庫技術及應用(第3版)
- Hadoop 2.X HDFS源碼剖析