Skip to content

Instantly share code, notes, and snippets.

@temoto
Created October 2, 2012 18:58
Show Gist options
  • Select an option

  • Save temoto/3822479 to your computer and use it in GitHub Desktop.

Select an option

Save temoto/3822479 to your computer and use it in GitHub Desktop.
Go index (for range) micro benchmark
BenchmarkStringIndex1First 100000000 20.0 ns/op
BenchmarkStringIndex2First 100000000 19.8 ns/op
BenchmarkStringIndex1Last 50000000 59.4 ns/op
BenchmarkStringIndex2Last 50000000 45.7 ns/op
ok indexbench 9.413s
package main
import (
"testing"
)
var WhitespaceChars = []rune{' ', '\t', '\v', 'a', 'z'}
func StringIndex1(ch rune) bool {
for i, _ := range WhitespaceChars {
if ch == WhitespaceChars[i] {
return true
}
}
return false
}
func StringIndex2(ch rune) bool {
for _, rune := range WhitespaceChars {
if ch == rune {
return true
}
}
return false
}
func BenchmarkStringIndex1First(b *testing.B) {
for i := 0; i < b.N; i++ {
StringIndex1(' ')
StringIndex1(' ')
StringIndex1(' ')
StringIndex1(' ')
StringIndex1(' ')
}
}
func BenchmarkStringIndex2First(b *testing.B) {
for i := 0; i < b.N; i++ {
StringIndex2(' ')
StringIndex2(' ')
StringIndex2(' ')
StringIndex2(' ')
StringIndex2(' ')
}
}
func BenchmarkStringIndex1Last(b *testing.B) {
for i := 0; i < b.N; i++ {
StringIndex1('z')
StringIndex1('z')
StringIndex1('z')
StringIndex1('z')
StringIndex1('z')
}
}
func BenchmarkStringIndex2Last(b *testing.B) {
for i := 0; i < b.N; i++ {
StringIndex2('z')
StringIndex2('z')
StringIndex2('z')
StringIndex2('z')
StringIndex2('z')
}
}
TEXT StringIndex1+0(SB),$16-16
MOVL ch+0(FP),R11
MOVQ WhitespaceChars+0(SB),R10
MOVL WhitespaceChars+8(SB),R9
MOVQ R10,autotmp_0000+-16(SP)
MOVL R9,autotmp_0000+-8(SP)
MOVL WhitespaceChars+12(SB),BX
MOVL BX,autotmp_0000+-4(SP)
MOVL $0,AX
MOVL autotmp_0000+-8(SP),DI
LEAQ autotmp_0000+-16(SP),BX
MOVQ (BX),CX
JMP ,14
INCL ,AX
CMPL AX,DI
JGE ,27
MOVL (CX),BP
ADDQ $4,CX
MOVLQSX AX,BP
CMPL BP,R9
JCS ,22
CALL ,runtime.panicindex+0(SB)
MOVL (R10)(BP*4),BX
CMPL BX,R11
JNE ,13
MOVB $1,.noname+8(FP)
RET ,
MOVB $0,.noname+8(FP)
RET ,
TEXT StringIndex2+0(SB),$16-16
MOVL ch+0(FP),DI
MOVQ WhitespaceChars+0(SB),BX
MOVQ BX,autotmp_0005+-16(SP)
MOVL WhitespaceChars+8(SB),BX
MOVL BX,autotmp_0005+-8(SP)
MOVL WhitespaceChars+12(SB),BX
MOVL BX,autotmp_0005+-4(SP)
MOVL $0,AX
MOVL autotmp_0005+-8(SP),SI
LEAQ autotmp_0005+-16(SP),BX
MOVQ (BX),CX
JMP ,43
INCL ,AX
CMPL AX,SI
JGE ,51
MOVL (CX),BP
ADDQ $4,CX
CMPL DI,BP
JNE ,42
MOVB $1,.noname+8(FP)
RET ,
MOVB $0,.noname+8(FP)
RET ,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment