My colleage told me that code modding 2's power will be optimized to bit operation and faster than modding other numbers. And I've checked the assembly which proves his option. But I wrote a benchmark code in Golang and run it with Go version 1.17. It seems that there is no much difference. Why does that happened and is he right?
Here is the Golang code:
package main
import (
"fmt"
"time"
)
const loop = 10000000000
func Mod1024() int {
sum := 0
for i := 0; i < loop; i++ {
sum += i % 1024
}
return sum
}
func Mod1023() int {
sum := 0
for i := 0; i < loop; i++ {
sum += i % 1023
}
return sum
}
func main() {
start := time.Now()
Mod1023()
fmt.Println(time.Since(start).Microseconds())
start = time.Now()
Mod1024()
fmt.Println(time.Since(start).Microseconds())
}
The result on my computer is:
2810668
2694136