- 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.
- Rust編程從入門到實戰
- Python高級機器學習
- Python數據分析(第2版)
- Python應用輕松入門
- Backbone.js Blueprints
- INSTANT OpenNMS Starter
- Drupal 8 Configuration Management
- Unity 5 for Android Essentials
- Android開發案例教程與項目實戰(在線實驗+在線自測)
- Python編程從0到1(視頻教學版)
- 計算機應用基礎案例教程
- Learning PHP 7
- Learning Modular Java Programming
- Mobile Forensics:Advanced Investigative Strategies
- Hands-On ROS for Robotics Programming