Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Benchmarking
func BenchmarkLoopOne(b *testing.B) {
; omitted
for i := 0; i < b.N; i++ {
0x10ef016 eb03 JMP 0x10ef01b
0x10ef018 48ffc0 INCQ AX
0x10ef01b 488b542458 MOVQ 0x58(SP), DX
0x10ef020 488b9af0000000 MOVQ 0xf0(DX), BX
0x10ef027 4839d8 CMPQ BX, AX
0x10ef02a 7d3c JGE 0x10ef068
; like the generated POPCNT code
if isSupported {
0x10ef02c 0fb61dcd9f0d00 MOVZX 0xd9fcd(IP), BX
0x10ef033 84db TESTL BL, BL
0x10ef035 7405 JE 0x10ef03c
cnt += i
0x10ef037 4801c1 ADDQ AX, CX
if isSupported {
0x10ef03a ebdc JMP 0x10ef018
; call a function if isSupported is false
0x10ef03c 4889442420 MOVQ AX, 0x20(SP)
0x10ef041 48894c2428 MOVQ CX, 0x28(SP)
cnt += someOtherFunc(i)
0x10ef046 48890424 MOVQ AX, 0(SP)
0x10ef04a e891ffffff CALL command-line-arguments.someOtherFunc(SB)
0x10ef04f 488b442408 MOVQ 0x8(SP), AX
0x10ef054 488b4c2428 MOVQ 0x28(SP), CX
0x10ef059 4801c1 ADDQ AX, CX
0x10ef05c 488b442420 MOVQ 0x20(SP), AX
0x10ef061 488b542458 MOVQ 0x58(SP), DX
if isSupported {
0x10ef066 ebb0 JMP 0x10ef018
BenchmarkLoopOne-8 2000000000 0.59 ns/op
BenchmarkLoopTwo-8 2000000000 0.39 ns/op
BenchmarkLoopThree-8 1000000000 1.99 ns/op
package main
import (
"testing"
)
var (
isSupported = true
)
//go:noinline
func someOtherFunc(v int) int {
return v
}
func BenchmarkLoopOne(b *testing.B) {
var cnt int
for i := 0; i < b.N; i++ {
if isSupported {
cnt += i
} else {
cnt += someOtherFunc(i)
}
}
b.Log(cnt)
}
func BenchmarkLoopTwo(b *testing.B) {
var cnt int
for i := 0; i < b.N; i++ {
cnt += i
}
b.Log(cnt)
}
func BenchmarkLoopThree(b *testing.B) {
var cnt int
for i := 0; i < b.N; i++ {
cnt += someOtherFunc(i)
}
b.Log(cnt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment