Skip to content

Instantly share code, notes, and snippets.

@akahn
Last active December 10, 2022 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akahn/c203a391b5251051b430ce237ee93e66 to your computer and use it in GitHub Desktop.
Save akahn/c203a391b5251051b430ce237ee93e66 to your computer and use it in GitHub Desktop.
package main
import "testing"
var testSlice = [2]byte{0xC6}
func rangeAccess() int {
sum := 0
for _, ch := range testSlice {
sum += int(ch)
}
return sum
}
func arrayAccess() int {
return int(testSlice[0]) + int(testSlice[1])
}
func BenchmarkArray(b *testing.B) {
for i := 0; i < b.N; i++ {
arrayAccess()
}
}
func BenchmarkRange(b *testing.B) {
for i := 0; i < b.N; i++ {
rangeAccess()
}
}
// BenchmarkArray-10 1000000000 0.3146 ns/op
// BenchmarkRange-10 625083966 1.888 ns/op
main.rangeAccess STEXT nosplit size=80 args=0x0 locals=0x18 funcid=0x0 align=0x0 leaf
0x0000 00000 (range_test.go:7) TEXT main.rangeAccess(SB), NOSPLIT|LEAF|ABIInternal, $32-0
0x0000 00000 (range_test.go:7) MOVD.W R30, -32(RSP)
0x0004 00004 (range_test.go:7) MOVD R29, -8(RSP)
0x0008 00008 (range_test.go:7) SUB $8, RSP, R29
0x000c 00012 (range_test.go:7) FUNCDATA ZR, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
0x000c 00012 (range_test.go:7) FUNCDATA $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
0x000c 00012 (range_test.go:7) PCDATA $0, $-3
0x000c 00012 (range_test.go:9) MOVHU main.testSlice(SB), R1
0x0018 00024 (range_test.go:9) PCDATA $0, $-1
0x0018 00024 (range_test.go:9) MOVH R1, main..autotmp_3-2(SP)
0x001c 00028 (range_test.go:9) MOVD ZR, R0
0x0020 00032 (range_test.go:9) MOVD ZR, R1
0x0024 00036 (range_test.go:9) JMP 56
0x0028 00040 (range_test.go:9) MOVD $main..autotmp_3-2(SP), R2
0x002c 00044 (range_test.go:9) MOVBU (R2)(R0), R2
0x0030 00048 (range_test.go:9) ADD $1, R0, R0
0x0034 00052 (range_test.go:10) ADD R1, R2, R1
0x0038 00056 (range_test.go:9) CMP $2, R0
0x003c 00060 (range_test.go:9) BLT 40
0x0040 00064 (range_test.go:12) MOVD R1, R0
0x0044 00068 (range_test.go:12) ADD $32, RSP
0x0048 00072 (range_test.go:12) SUB $8, RSP, R29
0x004c 00076 (range_test.go:12) RET (R30)
0x0000 fe 0f 1e f8 fd 83 1f f8 fd 23 00 d1 1b 00 00 90 .........#......
0x0010 7b 03 00 91 61 03 40 79 e1 2f 00 79 00 00 80 d2 {...a.@y./.y....
0x0020 01 00 80 d2 05 00 00 14 e2 5b 00 91 42 68 60 38 .........[..Bh`8
0x0030 00 04 00 91 41 00 01 8b 1f 08 00 f1 6b ff ff 54 ....A.......k..T
0x0040 e0 03 01 aa ff 83 00 91 fd 23 00 d1 c0 03 5f d6 .........#...._.
rel 12+8 t=3 main.testSlice+0
main.arrayAccess STEXT size=32 args=0x0 locals=0x0 funcid=0x0 align=0x0 leaf
0x0000 00000 (range_test.go:15) TEXT main.arrayAccess(SB), LEAF|NOFRAME|ABIInternal, $0-0
0x0000 00000 (range_test.go:15) FUNCDATA ZR, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
0x0000 00000 (range_test.go:15) FUNCDATA $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
0x0000 00000 (range_test.go:15) PCDATA $0, $-3
0x0000 00000 (range_test.go:16) MOVBU main.testSlice(SB), R1
0x000c 00012 (range_test.go:16) PCDATA $0, $-4
0x000c 00012 (range_test.go:16) MOVBU main.testSlice+1(SB), R2
0x0018 00024 (range_test.go:16) PCDATA $0, $-1
0x0018 00024 (range_test.go:16) ADD R2, R1, R0
0x001c 00028 (range_test.go:16) RET (R30)
0x0000 1b 00 00 90 7b 03 00 91 61 03 40 39 1b 00 00 90 ....{...a.@9....
0x0010 7b 03 00 91 62 03 40 39 20 00 02 8b c0 03 5f d6 {...b.@9 ....._.
rel 0+8 t=3 main.testSlice+0
rel 12+8 t=3 main.testSlice+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment