Created
October 2, 2012 18:58
-
-
Save temoto/3822479 to your computer and use it in GitHub Desktop.
Go index (for range) micro benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 , |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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