Wise bits
Short observation related to bitwise operations.
Frankly, I’m not using them very often, but occasionally decided to measure performance difference between regular pow
operation
and bitwise <<<
, because heard multiple times that bitwise operations perform better.
So, here is two samples:
- power of 2 using multiplication in loop(and example with Math.pow() for comparison)
@Benchmark
fun pow2(){
var result = 1
for (i in 1..power){
result *= 2
}
}
@Benchmark
fun pow2Math(){
Math.pow(2.0, power.toDouble())
}
- the same power of 2 using left unsigned shift
fun pow2bitwise(){
// This line equals 2^power
1 shl power
}
The whole benchmarks is here
Here are results of benchmark(pretty expected)
main summary:
Benchmark (power) Mode Cnt Score Error Units
BitwiseBenchMark.pow2 100000 avgt 3 24025.646 ± 2150.133 ns/op
BitwiseBenchMark.pow2 10000000 avgt 3 2391201.922 ± 199229.741 ns/op
BitwiseBenchMark.pow2Math 100000 avgt 3 8.757 ± 0.668 ns/op
BitwiseBenchMark.pow2Math 10000000 avgt 3 8.750 ± 0.455 ns/op
BitwiseBenchMark.pow2bitwise 100000 avgt 3 0.353 ± 2.009 ns/op
BitwiseBenchMark.pow2bitwise 10000000 avgt 3 0.356 ± 2.031 ns/op
- my ugly function with loop gives worse performance: O(n) time complexity
Math.pow()
performs better and show O(1) complexity- left shift
<<<
is faster then everything above.
Hope, it was interesting to know. See you! :-)