Skip to content

Instantly share code, notes, and snippets.

@iand
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iand/976c2182eb64de630568 to your computer and use it in GitHub Desktop.
Save iand/976c2182eb64de630568 to your computer and use it in GitHub Desktop.
Benchhmarking Bounds Checking of Slices
BenchmarkBoundsCheckRawLoop 200000 8782 ns/op 0 B/op 0 allocs/op
BenchmarkBoundsCheckRangeIndex 200000 9553 ns/op 0 B/op 0 allocs/op
BenchmarkBoundsCheckRangeVal 200000 8648 ns/op 0 B/op 0 allocs/op
package code
import (
"testing"
)
var y = 0
var bvalues = makeIntSlice(4000)
func makeIntSlice(n int) []int {
r := make([]int, n)
for i := 0; i < n; i++ {
r[i] = i
}
return r
}
func BoundsCheckRawLoop() {
for i := 0; i < len(bvalues); i++ {
y += bvalues[i]
}
}
func BoundsCheckRangeIndex() {
for i := range bvalues {
y += bvalues[i]
}
}
func BoundsCheckRangeVal() {
for _, i := range bvalues {
y += i
}
}
func BenchmarkBoundsCheckRawLoop(b *testing.B) {
for i := 0; i < b.N; i++ {
BoundsCheckRawLoop()
}
}
func BenchmarkBoundsCheckRangeIndex(b *testing.B) {
for i := 0; i < b.N; i++ {
BoundsCheckRangeIndex()
}
}
func BenchmarkBoundsCheckRangeVal(b *testing.B) {
for i := 0; i < b.N; i++ {
BoundsCheckRangeVal()
}
}
0x0000 00000 (bounds.go:21) TEXT "".BoundsCheckRangeIndex+0(SB),$0-0
0x0000 00000 (bounds.go:21) MOVQ (TLS),CX
0x0009 00009 (bounds.go:21) CMPQ SP,16(CX)
0x000d 00013 (bounds.go:21) JHI ,22
0x000f 00015 (bounds.go:21) CALL ,runtime.morestack_noctxt(SB)
0x0014 00020 (bounds.go:21) JMP ,0
0x0016 00022 (bounds.go:21) NOP ,
0x0016 00022 (bounds.go:21) NOP ,
0x0016 00022 (bounds.go:21) FUNCDATA $0,gclocals·3280bececceccd33cb74587feedb1f9f+0(SB)
0x0016 00022 (bounds.go:21) FUNCDATA $1,gclocals·3280bececceccd33cb74587feedb1f9f+0(SB)
0x0016 00022 (bounds.go:22) MOVQ "".bvalues+8(SB),SI
0x001d 00029 (bounds.go:22) MOVQ $0,AX
0x001f 00031 (bounds.go:22) CMPQ AX,SI
0x0022 00034 (bounds.go:22) JGE $0,87
0x0024 00036 (bounds.go:23) MOVQ "".y+0(SB),CX
0x002b 00043 (bounds.go:23) MOVQ "".bvalues+0(SB),BX
0x0032 00050 (bounds.go:23) MOVQ "".bvalues+8(SB),R8
0x0039 00057 (bounds.go:23) CMPQ AX,R8
0x003c 00060 (bounds.go:23) JCC $1,88
0x003e 00062 (bounds.go:23) LEAQ (BX)(AX*8),BX
0x0042 00066 (bounds.go:23) MOVQ (BX),BX
0x0045 00069 (bounds.go:23) ADDQ CX,BX
0x0048 00072 (bounds.go:23) MOVQ BX,"".y+0(SB)
0x004f 00079 (bounds.go:23) NOP ,
0x004f 00079 (bounds.go:22) INCQ ,AX
0x0052 00082 (bounds.go:22) CMPQ AX,SI
0x0055 00085 (bounds.go:22) JLT $0,36
0x0057 00087 (bounds.go:25) RET ,
0x0058 00088 (bounds.go:23) PCDATA $0,$0
0x0058 00088 (bounds.go:23) CALL ,runtime.panicindex(SB)
0x005d 00093 (bounds.go:23) UNDEF ,
0x0000 00000 (bounds.go:27) TEXT "".BoundsCheckRangeVal+0(SB),4,$0-0
0x0000 00000 (bounds.go:27) NOP ,
0x0000 00000 (bounds.go:27) NOP ,
0x0000 00000 (bounds.go:27) FUNCDATA $0,gclocals·3280bececceccd33cb74587feedb1f9f+0(SB)
0x0000 00000 (bounds.go:27) FUNCDATA $1,gclocals·3280bececceccd33cb74587feedb1f9f+0(SB)
0x0000 00000 (bounds.go:28) NOP ,
0x0000 00000 (bounds.go:28) MOVQ "".bvalues+0(SB),AX
0x0007 00007 (bounds.go:28) MOVQ "".bvalues+8(SB),SI
0x000e 00014 (bounds.go:28) MOVQ "".bvalues+16(SB),BX
0x0015 00021 (bounds.go:28) MOVQ $0,CX
0x0017 00023 (bounds.go:28) CMPQ CX,SI
0x001a 00026 (bounds.go:28) JGE $0,60
0x001c 00028 (bounds.go:28) NOP ,
0x001c 00028 (bounds.go:28) MOVQ (AX),DX
0x001f 00031 (bounds.go:29) MOVQ "".y+0(SB),BX
0x0026 00038 (bounds.go:29) ADDQ DX,BX
0x0029 00041 (bounds.go:29) MOVQ BX,"".y+0(SB)
0x0030 00048 (bounds.go:29) NOP ,
0x0030 00048 (bounds.go:28) ADDQ $8,AX
0x0034 00052 (bounds.go:28) INCQ ,CX
0x0037 00055 (bounds.go:28) CMPQ CX,SI
0x003a 00058 (bounds.go:28) JLT $0,28
0x003c 00060 (bounds.go:28) NOP ,
0x003c 00060 (bounds.go:31) RET ,
0x0000 00000 (bounds.go:15) TEXT "".BoundsCheckRawLoop+0(SB),$0-0
0x0000 00000 (bounds.go:15) MOVQ (TLS),CX
0x0009 00009 (bounds.go:15) CMPQ SP,16(CX)
0x000d 00013 (bounds.go:15) JHI ,22
0x000f 00015 (bounds.go:15) CALL ,runtime.morestack_noctxt(SB)
0x0014 00020 (bounds.go:15) JMP ,0
0x0016 00022 (bounds.go:15) NOP ,
0x0016 00022 (bounds.go:15) NOP ,
0x0016 00022 (bounds.go:15) FUNCDATA $0,gclocals·3280bececceccd33cb74587feedb1f9f+0(SB)
0x0016 00022 (bounds.go:15) FUNCDATA $1,gclocals·3280bececceccd33cb74587feedb1f9f+0(SB)
0x0016 00022 (bounds.go:16) MOVQ $0,AX
0x0018 00024 (bounds.go:16) MOVQ "".bvalues+8(SB),CX
0x001f 00031 (bounds.go:16) CMPQ AX,CX
0x0022 00034 (bounds.go:16) JGE $0,94
0x0024 00036 (bounds.go:16) NOP ,
0x0024 00036 (bounds.go:17) MOVQ "".y+0(SB),CX
0x002b 00043 (bounds.go:17) MOVQ "".bvalues+0(SB),BX
0x0032 00050 (bounds.go:17) MOVQ "".bvalues+8(SB),R8
0x0039 00057 (bounds.go:17) CMPQ AX,R8
0x003c 00060 (bounds.go:17) JCC $1,95
0x003e 00062 (bounds.go:17) LEAQ (BX)(AX*8),BX
0x0042 00066 (bounds.go:17) MOVQ (BX),BX
0x0045 00069 (bounds.go:17) ADDQ CX,BX
0x0048 00072 (bounds.go:17) MOVQ BX,"".y+0(SB)
0x004f 00079 (bounds.go:17) NOP ,
0x004f 00079 (bounds.go:16) INCQ ,AX
0x0052 00082 (bounds.go:16) NOP ,
0x0052 00082 (bounds.go:16) MOVQ "".bvalues+8(SB),CX
0x0059 00089 (bounds.go:16) CMPQ AX,CX
0x005c 00092 (bounds.go:16) JLT $0,36
0x005e 00094 (bounds.go:16) NOP ,
0x005e 00094 (bounds.go:19) RET ,
0x005f 00095 (bounds.go:17) PCDATA $0,$0
0x005f 00095 (bounds.go:17) CALL ,runtime.panicindex(SB)
0x0064 00100 (bounds.go:17) UNDEF ,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment