- 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.
- Learning Neo4j
- Visual Basic .NET程序設計(第3版)
- Raspberry Pi Networking Cookbook(Second Edition)
- SQL Server 2012數據庫技術及應用(微課版·第5版)
- Developing Middleware in Java EE 8
- Reactive Programming With Java 9
- Python 3破冰人工智能:從入門到實戰
- FLL+WRO樂高機器人競賽教程:機械、巡線與PID
- JavaScript:Moving to ES2015
- Python High Performance Programming
- 用戶體驗可視化指南
- Azure Serverless Computing Cookbook
- Python趣味編程與精彩實例
- 大數據時代的企業升級之道(全3冊)
- Java Web開發教程:基于Struts2+Hibernate+Spring