达到相同目的,可以有多种写法,每种写法有性能、可读性方面的区别,本文旨在探讨不同写法之间的性能差异
len(str) vs str == “”
本部分参考自:
问个 Go 问题,字符串 len == 0 和 字符串== “” ,有啥区别?
gotest.go:
package gotest
func Test1() bool {
var v string
if v == "" {
return true
}
return false
}
func Test2() bool {
var v string
if len(v) == 0 {
return true
}
return false
}
webbench_test.go:
package gotest
import (
"testing"
)
func BenchmarkTest1(b *testing.B) {
for i := 0; i
执行 go test -test.bench=".*"
goos: darwin
goarch: amd64
pkg: note/performance
BenchmarkTest1-8 1000000000 0.467 ns/op
BenchmarkTest2-8 1000000000 0.464 ns/op
PASS
ok note/performance 1.290s
第4行显示了BenchmarkTest1执行了1000000000次,每次的执行平均时间是0.467纳秒,
第5行显示了BenchmarkTest2也执行了1000000000次,每次的平均执行时间是0.464 纳秒。
最后一行显示总共的执行时间为 1.290s
可使用-count
来指定执行多少次 go test -test.bench=".*" -count=5
:
goos: darwin
goarch: amd64
pkg: note/performance
BenchmarkTest1-8 1000000000 0.485 ns/op
BenchmarkTest1-8 1000000000 0.484 ns/op
BenchmarkTest1-8 1000000000 0.464 ns/op
BenchmarkTest1-8 1000000000 0.497 ns/op
BenchmarkTest1-8 1000000000 0.479 ns/op
BenchmarkTest2-8 1000000000 0.490 ns/op
BenchmarkTest2-8 1000000000 0.476 ns/op
BenchmarkTest2-8 1000000000 0.482 ns/op
BenchmarkTest2-8 1000000000 0.469 ns/op
BenchmarkTest2-8 1000000000 0.474 ns/op
PASS
ok note/performance 5.791s
go test --bench=. -benchmem
(添加 -benchmem
参数,可以提供每次操作分配内存的次数,以及每次操作分配的字节数。参考 go benchmark 性能测试)
goos: darwin
goarch: amd64
pkg: note/performance
BenchmarkTest1-8 1000000000 0.471 ns/op 0 B/op 0 allocs/op
BenchmarkTest2-8 1000000000 0.462 ns/op 0 B/op 0 allocs/op
PASS
ok note/performance 1.457s
经过多次测试,可知:
. 性能几乎没有差别
. 均不涉及内存申请和操作,均为 0 allocs/op。(也说明变量并不是声明了,就有初始化动作. Go 编译器有做优化)
进一步看两者的汇编代码,以细究具体区别在哪里:
go tool compile -S gotest.go:
"".Test1 STEXT nosplit size=6 args=0x8 locals=0x0
0x0000 00000 (gotest.go:3) TEXT "".Test1(SB), NOSPLIT|ABIInternal, $0-8
0x0000 00000 (gotest.go:3) PCDATA $0, $-2
0x0000 00000 (gotest.go:3) PCDATA $1, $-2
0x0000 00000 (gotest.go:3) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (gotest.go:3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (gotest.go:3) FUNCDATA $2, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (gotest.go:6) PCDATA $0, $0
0x0000 00000 (gotest.go:6) PCDATA $1, $0
0x0000 00000 (gotest.go:6) MOVB $1, "".~r0+8(SP)
0x0005 00005 (gotest.go:6) RET
0x0000 c6 44 24 08 01 c3 .D$...
"".Test2 STEXT nosplit size=6 args=0x8 locals=0x0
0x0000 00000 (gotest.go:11) TEXT "".Test2(SB), NOSPLIT|ABIInternal, $0-8
0x0000 00000 (gotest.go:11) PCDATA $0, $-2
0x0000 00000 (gotest.go:11) PCDATA $1, $-2
0x0000 00000 (gotest.go:11) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (gotest.go:11) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (gotest.go:11) FUNCDATA $2, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (gotest.go:14) PCDATA $0, $0
0x0000 00000 (gotest.go:14) PCDATA $1, $0
0x0000 00000 (gotest.go:14) MOVB $1, "".~r0+8(SP)
0x0005 00005 (gotest.go:14) RET
0x0000 c6 44 24 08 01 c3 .D$...
go.cuinfo.packagename. SDWARFINFO dupok size=0
0x0000 67 6f 74 65 73 74 gotest
go.loc."".Test1 SDWARFLOC size=0
go.info."".Test1 SDWARFINFO size=46
0x0000 03 22 22 2e 54 65 73 74 31 00 00 00 00 00 00 00 ."".Test1.......
0x0010 00 00 00 00 00 00 00 00 00 00 01 9c 00 00 00 00 ................
0x0020 01 0f 7e 72 30 00 01 03 00 00 00 00 00 00 ..~r0.........
rel 0+0 t=24 type.bool+0
rel 10+8 t=1 "".Test1+0
rel 18+8 t=1 "".Test1+6
rel 28+4 t=30 gofile../Users/dashen/go/src/note/performance/gotest.go+0
rel 40+4 t=29 go.info.bool+0
go.range."".Test1 SDWARFRANGE size=0
go.debuglines."".Test1 SDWARFMISC size=11
0x0000 04 02 14 06 41 04 01 03 7b 06 01 ....A...{..
go.loc."".Test2 SDWARFLOC size=0
go.info."".Test2 SDWARFINFO size=46
0x0000 03 22 22 2e 54 65 73 74 32 00 00 00 00 00 00 00 ."".Test2.......
0x0010 00 00 00 00 00 00 00 00 00 00 01 9c 00 00 00 00 ................
0x0020 01 0f 7e 72 30 00 01 0b 00 00 00 00 00 00 ..~r0.........
rel 0+0 t=24 type.bool+0
rel 10+8 t=1 "".Test2+0
rel 18+8 t=1 "".Test2+6
rel 28+4 t=30 gofile../Users/dashen/go/src/note/performance/gotest.go+0
rel 40+4 t=29 go.info.bool+0
go.range."".Test2 SDWARFRANGE size=0
go.debuglines."".Test2 SDWARFMISC size=13
0x0000 04 02 03 08 14 06 41 04 01 03 73 06 01 ......A...s..
gclocals·33cdeccccebe80329f1fdbee7f5874cb SRODATA dupok size=8
0x0000 01 00 00 00 00 00 00 00 ........
编译出来的汇编代码是完全一致的,可以明确 Go 编译器对此做了优化(应该是直接比对了)
—– EOF选看: —–
生成pprof:
go test -bench=".*" -cpuprofile=cpu.profile ../xxx文件夹
此时会在文件夹下,生成一个 xxx.test
go tool pprof xxx.test cpu.profile
:
File: performance.test
Type: cpu
Time: Apr 12, 2021 at 5:20pm (CST)
Duration: 1.23s, Total samples = 970ms (78.99%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
(pprof) o
call_tree = false
compact_labels = true
cumulative = flat //: [cum | flat]
divide_by = 1
drop_negative = false
edgefraction = 0.001
focus = ""
granularity = filefunctions //: [addresses | filefunctions | files | functions | lines]
hide = ""
ignore = ""
mean = false
nodecount = -1 //: default
nodefraction = 0.005
noinlines = false
normalize = false
output = ""
prune_from = ""
relative_percentages = false
sample_index = cpu //: [samples | cpu]
show = ""
show_from = ""
tagfocus = ""
taghide = ""
tagignore = ""
tagshow = ""
trim = true
trim_path = ""
unit = minimum
执行 go tool pprof -web xxx.test cpu.profile
—– EOF —–
几种 int转string 方法的性能差异
package shuang
import (
"fmt"
"strconv"
"testing"
)
func BenchmarkSprintf(b *testing.B) {
n := 10
b.ResetTimer()
for i := 0; i
执行 go test -test.bench=".*" -benchmem
goos: darwin
goarch: amd64
pkg: dashen
BenchmarkSprintf-8 14417409 75.9 ns/op 16 B/op 2 allocs/op
BenchmarkItoa-8 452276205 2.64 ns/op 0 B/op 0 allocs/op
BenchmarkFormatInt-8 492620018 2.42 ns/op 0 B/op 0 allocs/op
PASS
ok dashen 4.518s
第4行显示了BenchmarkSprintf-8 执行了14417409次,每次的执行平均时间是75.9纳秒, 每次操作有两次内存分配,每次分配了16Byte大小的内存空间
第5行显示了BenchmarkItoa-8 执行了452276205次,每次的平均执行时间是2.64 纳秒, 无内存分配
第6行显示了BenchmarkFormatInt-8 执行了492620018次,每次的平均执行时间是2.42 纳秒, 无内存分配 。
最后一行显示总共的执行时间为 4.518s
可见, strconv.FormatInt(n, 10)
和 strconv.Itoa(n)
性能差不多, fmt.Sprintf()
性能最差
Golang 中整数转字符串
几种 字符串拼接 写法的性能差异
将两个字符串 “hello”和”world”,拼接为”hello,world”
package shuang
import (
"bytes"
"fmt"
"strings"
"testing"
)
func BenchmarkAddStringWithOperator(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i
执行 go test -test.bench=".*" -benchmem
goos: darwin
goarch: amd64
pkg: dashen
BenchmarkAddStringWithOperator-8 52448029 21.4 ns/op 0 B/op 0 allocs/op
BenchmarkAddStringWithSprintf-8 8755447 136 ns/op 48 B/op 3 allocs/op
BenchmarkAddStringWithJoin-8 31878931 37.0 ns/op 16 B/op 1 allocs/op
BenchmarkAddStringWithBuffer-8 1000000000 0.000104 ns/op 0 B/op 0 allocs/op
PASS
ok dashen 4.420s
第4行显示了BenchmarkAddStringWithOperator-8 执行了52448029次,每次的执行平均时间是 21.4纳秒, 无内存分配
第5行显示了BenchmarkAddStringWithSprintf-8 执行了8755447次,每次的平均执行时间是 136纳秒, 每次操作有3次内存分配,每次分配了48Byte大小的内存空间
第6行显示了BenchmarkAddStringWithJoin-8 执行了31878931次,每次的平均执行时间是 37.0纳秒, 每次操作有1次内存分配,每次分配了16Byte大小的内存空间
第7行显示了BenchmarkAddStringWithBuffer-8 执行了1000000000次,每次的平均执行时间是 0.000104纳秒, 无内存分配
最后一行显示总共的执行时间为 4.420s
可见, fmt.Sprintf()
和strings.Join()
均有内存分配,buffer.WriteString()
性能最好
go tool compile -S gotest.go:
看四种方法的汇编代码:"".BenchmarkAddStringWithOperator STEXT size=164 args=0x8 locals=0x78
0x0000 00000 (join_test.go:10) TEXT "".BenchmarkAddStringWithOperator(SB), ABIInternal, $120-8
0x0000 00000 (join_test.go:10) MOVQ (TLS), CX
0x0009 00009 (join_test.go:10) CMPQ SP, 16(CX)
0x000d 00013 (join_test.go:10) PCDATA $0, $-2
0x000d 00013 (join_test.go:10) JLS 154
0x0013 00019 (join_test.go:10) PCDATA $0, $-1
0x0013 00019 (join_test.go:10) SUBQ $120, SP
0x0017 00023 (join_test.go:10) MOVQ BP, 112(SP)
0x001c 00028 (join_test.go:10) LEAQ 112(SP), BP
0x0021 00033 (join_test.go:10) PCDATA $0, $-2
0x0021 00033 (join_test.go:10) PCDATA $1, $-2
0x0021 00033 (join_test.go:10) FUNCDATA $0, gclocals·a36216b97439c93dafebe03e7f0808b5(SB)
0x0021 00033 (join_test.go:10) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0021 00033 (join_test.go:10) FUNCDATA $2, gclocals·96839595c383af6ae8227769d90a999e(SB)
0x0021 00033 (join_test.go:10) PCDATA $0, $0
0x0021 00033 (join_test.go:10) PCDATA $1, $0
0x0021 00033 (join_test.go:10) XORL AX, AX
0x0023 00035 (join_test.go:13) JMP 127
0x0025 00037 (join_test.go:13) MOVQ AX, "".i+72(SP)
0x002a 00042 (join_test.go:14) PCDATA $0, $1
0x002a 00042 (join_test.go:14) LEAQ ""..autotmp_4+80(SP), AX
0x002f 00047 (join_test.go:14) PCDATA $0, $0
0x002f 00047 (join_test.go:14) MOVQ AX, (SP)
0x0033 00051 (join_test.go:14) PCDATA $0, $1
0x0033 00051 (join_test.go:14) LEAQ go.string."hello"(SB), AX
0x003a 00058 (join_test.go:14) PCDATA $0, $0
0x003a 00058 (join_test.go:14) MOVQ AX, 8(SP)
0x003f 00063 (join_test.go:14) MOVQ $5, 16(SP)
0x0048 00072 (join_test.go:14) PCDATA $0, $2
0x0048 00072 (join_test.go:14) LEAQ go.string.","(SB), CX
0x004f 00079 (join_test.go:14) PCDATA $0, $0
0x004f 00079 (join_test.go:14) MOVQ CX, 24(SP)
0x0054 00084 (join_test.go:14) MOVQ $1, 32(SP)
0x005d 00093 (join_test.go:14) PCDATA $0, $3
0x005d 00093 (join_test.go:14) LEAQ go.string."world"(SB), DX
0x0064 00100 (join_test.go:14) PCDATA $0, $0
0x0064 00100 (join_test.go:14) MOVQ DX, 40(SP)
0x0069 00105 (join_test.go:14) MOVQ $5, 48(SP)
0x0072 00114 (join_test.go:14) CALL runtime.concatstring3(SB)
0x0077 00119 (join_test.go:13) MOVQ "".i+72(SP), AX
0x007c 00124 (join_test.go:13) INCQ AX
0x007f 00127 (join_test.go:13) PCDATA $0, $2
0x007f 00127 (join_test.go:13) MOVQ "".b+128(SP), CX
0x0087 00135 (join_test.go:13) PCDATA $0, $0
0x0087 00135 (join_test.go:13) CMPQ 272(CX), AX
0x008e 00142 (join_test.go:13) JGT 37
0x0090 00144 (join_test.go:13) PCDATA $0, $-1
0x0090 00144 (join_test.go:13) PCDATA $1, $-1
0x0090 00144 (join_test.go:13) MOVQ 112(SP), BP
0x0095 00149 (join_test.go:13) ADDQ $120, SP
0x0099 00153 (join_test.go:13) RET
0x009a 00154 (join_test.go:13) NOP
0x009a 00154 (join_test.go:10) PCDATA $1, $-1
0x009a 00154 (join_test.go:10) PCDATA $0, $-2
0x009a 00154 (join_test.go:10) CALL runtime.morestack_noctxt(SB)
0x009f 00159 (join_test.go:10) PCDATA $0, $-1
0x009f 00159 (join_test.go:10) JMP 0
0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 0f 86 87 eH..%....H;a....
0x0010 00 00 00 48 83 ec 78 48 89 6c 24 70 48 8d 6c 24 ...H..xH.l$pH.l$
0x0020 70 31 c0 eb 5a 48 89 44 24 48 48 8d 44 24 50 48 p1..ZH.D$HH.D$PH
0x0030 89 04 24 48 8d 05 00 00 00 00 48 89 44 24 08 48 ..$H......H.D$.H
0x0040 c7 44 24 10 05 00 00 00 48 8d 0d 00 00 00 00 48 .D$.....H......H
0x0050 89 4c 24 18 48 c7 44 24 20 01 00 00 00 48 8d 15 .L$.H.D$ ....H..
0x0060 00 00 00 00 48 89 54 24 28 48 c7 44 24 30 05 00 ....H.T$(H.D$0..
0x0070 00 00 e8 00 00 00 00 48 8b 44 24 48 48 ff c0 48 .......H.D$HH..H
0x0080 8b 8c 24 80 00 00 00 48 39 81 10 01 00 00 7f 95 ..$....H9.......
0x0090 48 8b 6c 24 70 48 83 c4 78 c3 e8 00 00 00 00 e9 H.l$pH..x.......
0x00a0 5c ff ff ff ...
rel 5+4 t=17 TLS+0
rel 54+4 t=16 go.string."hello"+0
rel 75+4 t=16 go.string.","+0
rel 96+4 t=16 go.string."world"+0
rel 115+4 t=8 runtime.concatstring3+0
rel 155+4 t=8 runtime.morestack_noctxt+0
"".BenchmarkAddStringWithSprintf STEXT size=254 args=0x8 locals=0x70
0x0000 00000 (join_test.go:18) TEXT "".BenchmarkAddStringWithSprintf(SB), ABIInternal, $112-8
0x0000 00000 (join_test.go:18) MOVQ (TLS), CX
0x0009 00009 (join_test.go:18) CMPQ SP, 16(CX)
0x000d 00013 (join_test.go:18) PCDATA $0, $-2
0x000d 00013 (join_test.go:18) JLS 244
0x0013 00019 (join_test.go:18) PCDATA $0, $-1
0x0013 00019 (join_test.go:18) SUBQ $112, SP
0x0017 00023 (join_test.go:18) MOVQ BP, 104(SP)
0x001c 00028 (join_test.go:18) LEAQ 104(SP), BP
0x0021 00033 (join_test.go:18) PCDATA $0, $-2
0x0021 00033 (join_test.go:18) PCDATA $1, $-2
0x0021 00033 (join_test.go:18) FUNCDATA $0, gclocals·69a9291448fa273f79569cb593f615b2(SB)
0x0021 00033 (join_test.go:18) FUNCDATA $1, gclocals·306ea4dc593c4841cf74b8270a1a332d(SB)
0x0021 00033 (join_test.go:18) FUNCDATA $2, gclocals·a5a3f3e663b1ae9c2739369df7a1eae5(SB)
0x0021 00033 (join_test.go:18) FUNCDATA $3, "".BenchmarkAddStringWithSprintf.stkobj(SB)
0x0021 00033 (join_test.go:18) PCDATA $0, $0
0x0021 00033 (join_test.go:18) PCDATA $1, $0
0x0021 00033 (join_test.go:18) XORL AX, AX
0x0023 00035 (join_test.go:21) JMP 216
0x0028 00040 (join_test.go:21) MOVQ AX, "".i+56(SP)
0x002d 00045 (join_test.go:22) PCDATA $0, $1
0x002d 00045 (join_test.go:22) LEAQ go.string."hello"(SB), AX
0x0034 00052 (join_test.go:22) PCDATA $0, $0
0x0034 00052 (join_test.go:22) MOVQ AX, (SP)
0x0038 00056 (join_test.go:22) MOVQ $5, 8(SP)
0x0041 00065 (join_test.go:22) CALL runtime.convTstring(SB)
0x0046 00070 (join_test.go:22) PCDATA $0, $1
0x0046 00070 (join_test.go:22) MOVQ 16(SP), AX
0x004b 00075 (join_test.go:22) PCDATA $0, $0
0x004b 00075 (join_test.go:22) PCDATA $1, $1
0x004b 00075 (join_test.go:22) MOVQ AX, ""..autotmp_13+64(SP)
0x0050 00080 (join_test.go:22) PCDATA $0, $2
0x0050 00080 (join_test.go:22) LEAQ go.string."world"(SB), CX
0x0057 00087 (join_test.go:22) PCDATA $0, $0
0x0057 00087 (join_test.go:22) MOVQ CX, (SP)
0x005b 00091 (join_test.go:22) MOVQ $5, 8(SP)
0x0064 00100 (join_test.go:22) CALL runtime.convTstring(SB)
0x0069 00105 (join_test.go:22) PCDATA $0, $1
0x0069 00105 (join_test.go:22) MOVQ 16(SP), AX
0x006e 00110 (join_test.go:22) PCDATA $1, $2
0x006e 00110 (join_test.go:22) XORPS X0, X0
0x0071 00113 (join_test.go:22) MOVUPS X0, ""..autotmp_4+72(SP)
0x0076 00118 (join_test.go:22) MOVUPS X0, ""..autotmp_4+88(SP)
0x007b 00123 (join_test.go:22) PCDATA $0, $3
0x007b 00123 (join_test.go:22) LEAQ type.string(SB), CX
0x0082 00130 (join_test.go:22) MOVQ CX, ""..autotmp_4+72(SP)
0x0087 00135 (join_test.go:22) PCDATA $0, $4
0x0087 00135 (join_test.go:22) PCDATA $1, $3
0x0087 00135 (join_test.go:22) MOVQ ""..autotmp_13+64(SP), DX
0x008c 00140 (join_test.go:22) PCDATA $0, $3
0x008c 00140 (join_test.go:22) MOVQ DX, ""..autotmp_4+80(SP)
0x0091 00145 (join_test.go:22) PCDATA $0, $1
0x0091 00145 (join_test.go:22) MOVQ CX, ""..autotmp_4+88(SP)
0x0096 00150 (join_test.go:22) PCDATA $0, $0
0x0096 00150 (join_test.go:22) MOVQ AX, ""..autotmp_4+96(SP)
0x009b 00155 (join_test.go:22) PCDATA $0, $1
0x009b 00155 (join_test.go:22) LEAQ go.string."%s,%s"(SB), AX
0x00a2 00162 (join_test.go:22) PCDATA $0, $0
0x00a2 00162 (join_test.go:22) MOVQ AX, (SP)
0x00a6 00166 (join_test.go:22) MOVQ $5, 8(SP)
0x00af 00175 (join_test.go:22) PCDATA $0, $5
0x00af 00175 (join_test.go:22) PCDATA $1, $0
0x00af 00175 (join_test.go:22) LEAQ ""..autotmp_4+72(SP), DX
0x00b4 00180 (join_test.go:22) PCDATA $0, $0
0x00b4 00180 (join_test.go:22) MOVQ DX, 16(SP)
0x00b9 00185 (join_test.go:22) MOVQ $2, 24(SP)
0x00c2 00194 (join_test.go:22) MOVQ $2, 32(SP)
0x00cb 00203 (join_test.go:22) CALL fmt.Sprintf(SB)
0x00d0 00208 (join_test.go:21) MOVQ "".i+56(SP), AX
0x00d5 00213 (join_test.go:21) INCQ AX
0x00d8 00216 (join_test.go:21) PCDATA $0, $2
0x00d8 00216 (join_test.go:21) MOVQ "".b+120(SP), CX
0x00dd 00221 (join_test.go:21) PCDATA $0, $0
0x00dd 00221 (join_test.go:21) CMPQ 272(CX), AX
0x00e4 00228 (join_test.go:21) JGT 40
0x00ea 00234 (join_test.go:21) PCDATA $0, $-1
0x00ea 00234 (join_test.go:21) PCDATA $1, $-1
0x00ea 00234 (join_test.go:21) MOVQ 104(SP), BP
0x00ef 00239 (join_test.go:21) ADDQ $112, SP
0x00f3 00243 (join_test.go:21) RET
0x00f4 00244 (join_test.go:21) NOP
0x00f4 00244 (join_test.go:18) PCDATA $1, $-1
0x00f4 00244 (join_test.go:18) PCDATA $0, $-2
0x00f4 00244 (join_test.go:18) CALL runtime.morestack_noctxt(SB)
0x00f9 00249 (join_test.go:18) PCDATA $0, $-1
0x00f9 00249 (join_test.go:18) JMP 0
0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 0f 86 e1 eH..%....H;a....
0x0010 00 00 00 48 83 ec 70 48 89 6c 24 68 48 8d 6c 24 ...H..pH.l$hH.l$
0x0020 68 31 c0 e9 b0 00 00 00 48 89 44 24 38 48 8d 05 h1......H.D$8H..
0x0030 00 00 00 00 48 89 04 24 48 c7 44 24 08 05 00 00 ....H..$H.D$....
0x0040 00 e8 00 00 00 00 48 8b 44 24 10 48 89 44 24 40 ......H.D$.H.D$@
0x0050 48 8d 0d 00 00 00 00 48 89 0c 24 48 c7 44 24 08 H......H..$H.D$.
0x0060 05 00 00 00 e8 00 00 00 00 48 8b 44 24 10 0f 57 .........H.D$..W
0x0070 c0 0f 11 44 24 48 0f 11 44 24 58 48 8d 0d 00 00 ...D$H..D$XH....
0x0080 00 00 48 89 4c 24 48 48 8b 54 24 40 48 89 54 24 ..H.L$HH.T$@H.T$
0x0090 50 48 89 4c 24 58 48 89 44 24 60 48 8d 05 00 00 PH.L$XH.D$`H....
0x00a0 00 00 48 89 04 24 48 c7 44 24 08 05 00 00 00 48 ..H..$H.D$.....H
0x00b0 8d 54 24 48 48 89 54 24 10 48 c7 44 24 18 02 00 .T$HH.T$.H.D$...
0x00c0 00 00 48 c7 44 24 20 02 00 00 00 e8 00 00 00 00 ..H.D$ .........
0x00d0 48 8b 44 24 38 48 ff c0 48 8b 4c 24 78 48 39 81 H.D$8H..H.L$xH9.
0x00e0 10 01 00 00 0f 8f 3e ff ff ff 48 8b 6c 24 68 48 ......>...H.l$hH
0x00f0 83 c4 70 c3 e8 00 00 00 00 e9 02 ff ff ff ..p...........
rel 5+4 t=17 TLS+0
rel 48+4 t=16 go.string."hello"+0
rel 66+4 t=8 runtime.convTstring+0
rel 83+4 t=16 go.string."world"+0
rel 101+4 t=8 runtime.convTstring+0
rel 126+4 t=16 type.string+0
rel 158+4 t=16 go.string."%s,%s"+0
rel 204+4 t=8 fmt.Sprintf+0
rel 245+4 t=8 runtime.morestack_noctxt+0
type..eq.[2]interface {} STEXT dupok size=179 args=0x18 locals=0x30
0x0000 00000 (:1) TEXT type..eq.[2]interface {}(SB), DUPOK|ABIInternal, $48-24
0x0000 00000 (:1) MOVQ (TLS), CX
0x0009 00009 (:1) CMPQ SP, 16(CX)
0x000d 00013 (:1) PCDATA $0, $-2
0x000d 00013 (:1) JLS 169
0x0013 00019 (:1) PCDATA $0, $-1
0x0013 00019 (:1) SUBQ $48, SP
0x0017 00023 (:1) MOVQ BP, 40(SP)
0x001c 00028 (:1) LEAQ 40(SP), BP
0x0021 00033 (:1) PCDATA $0, $-2
0x0021 00033 (:1) PCDATA $1, $-2
0x0021 00033 (:1) FUNCDATA $0, gclocals·dc9b0298814590ca3ffc3a889546fc8b(SB)
0x0021 00033 (:1) FUNCDATA $1, gclocals·69c1753bd5f81501d95132d08af04464(SB)
0x0021 00033 (:1) FUNCDATA $2, gclocals·313a5bdbfadc4f007c002a3a3588596d(SB)
0x0021 00033 (:1) PCDATA $0, $1
0x0021 00033 (:1) PCDATA $1, $0
0x0021 00033 (:1) MOVQ "".p+56(SP), AX
0x0026 00038 (:1) PCDATA $0, $2
0x0026 00038 (:1) MOVQ "".q+64(SP), CX
0x002b 00043 (:1) XORL DX, DX
0x002d 00045 (:1) JMP 72
0x002f 00047 (:1) PCDATA $0, $0
0x002f 00047 (:1) MOVQ ""..autotmp_8+32(SP), BX
0x0034 00052 (:1) LEAQ 1(BX), DX
0x0038 00056 (:1) PCDATA $0, $3
0x0038 00056 (:1) MOVQ "".p+56(SP), BX
0x003d 00061 (:1) PCDATA $0, $4
0x003d 00061 (:1) MOVQ "".q+64(SP), SI
0x0042 00066 (:1) PCDATA $0, $5
0x0042 00066 (:1) MOVQ BX, AX
0x0045 00069 (:1) PCDATA $0, $2
0x0045 00069 (:1) MOVQ SI, CX
0x0048 00072 (:1) CMPQ DX, $2
0x004c 00076 (:1) JGE 154
0x004e 00078 (:1) MOVQ DX, BX
0x0051 00081 (:1) SHLQ $4, DX
0x0055 00085 (:1) PCDATA $0, $6
0x0055 00085 (:1) MOVQ 8(DX)(AX*1), SI
0x005a 00090 (:1) PCDATA $0, $7
0x005a 00090 (:1) MOVQ (DX)(AX*1), DI
0x005e 00094 (:1) MOVQ (DX)(CX*1), R8
0x0062 00098 (:1) PCDATA $0, $8
0x0062 00098 (:1) MOVQ 8(DX)(CX*1), DX
0x0067 00103 (:1) CMPQ DI, R8
0x006a 00106 (:1) JNE 139
0x006c 00108 (:1) MOVQ BX, ""..autotmp_8+32(SP)
0x0071 00113 (:1) MOVQ DI, (SP)
0x0075 00117 (:1) PCDATA $0, $9
0x0075 00117 (:1) MOVQ SI, 8(SP)
0x007a 00122 (:1) PCDATA $0, $0
0x007a 00122 (:1) MOVQ DX, 16(SP)
0x007f 00127 (:1) CALL runtime.efaceeq(SB)
0x0084 00132 (:1) CMPB 24(SP), $0
0x0089 00137 (:1) JNE 47
0x008b 00139 (:1) PCDATA $1, $1
0x008b 00139 (:1) MOVB $0, "".~r2+72(SP)
0x0090 00144 (:1) MOVQ 40(SP), BP
0x0095 00149 (:1) ADDQ $48, SP
0x0099 00153 (:1) RET
0x009a 00154 (:1) MOVB $1, "".~r2+72(SP)
0x009f 00159 (:1) MOVQ 40(SP), BP
0x00a4 00164 (:1) ADDQ $48, SP
0x00a8 00168 (:1) RET
0x00a9 00169 (:1) NOP
0x00a9 00169 (:1) PCDATA $1, $-1
0x00a9 00169 (:1) PCDATA $0, $-2
0x00a9 00169 (:1) CALL runtime.morestack_noctxt(SB)
0x00ae 00174 (:1) PCDATA $0, $-1
0x00ae 00174 (:1) JMP 0
0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 0f 86 96 eH..%....H;a....
0x0010 00 00 00 48 83 ec 30 48 89 6c 24 28 48 8d 6c 24 ...H..0H.l$(H.l$
0x0020 28 48 8b 44 24 38 48 8b 4c 24 40 31 d2 eb 19 48 (H.D$8H.L$@1...H
0x0030 8b 5c 24 20 48 8d 53 01 48 8b 5c 24 38 48 8b 74 .$ H.S.H.$8H.t
0x0040 24 40 48 89 d8 48 89 f1 48 83 fa 02 7d 4c 48 89 $@H..H..H...}LH.
0x0050 d3 48 c1 e2 04 48 8b 74 02 08 48 8b 3c 02 4c 8b .H...H.t..H.:1) TEXT type..eq.[2]string(SB), DUPOK|ABIInternal, $48-24
0x0000 00000 (:1) MOVQ (TLS), CX
0x0009 00009 (:1) CMPQ SP, 16(CX)
0x000d 00013 (:1) PCDATA $0, $-2
0x000d 00013 (:1) JLS 168
0x0013 00019 (:1) PCDATA $0, $-1
0x0013 00019 (:1) SUBQ $48, SP
0x0017 00023 (:1) MOVQ BP, 40(SP)
0x001c 00028 (:1) LEAQ 40(SP), BP
0x0021 00033 (:1) PCDATA $0, $-2
0x0021 00033 (:1) PCDATA $1, $-2
0x0021 00033 (:1) FUNCDATA $0, gclocals·dc9b0298814590ca3ffc3a889546fc8b(SB)
0x0021 00033 (:1) FUNCDATA $1, gclocals·69c1753bd5f81501d95132d08af04464(SB)
0x0021 00033 (:1) FUNCDATA $2, gclocals·8a866e72a2964e4d3575e01f98e6fe5f(SB)
0x0021 00033 (:1) PCDATA $0, $1
0x0021 00033 (:1) PCDATA $1, $0
0x0021 00033 (:1) MOVQ "".p+56(SP), AX
0x0026 00038 (:1) PCDATA $0, $2
0x0026 00038 (:1) MOVQ "".q+64(SP), CX
0x002b 00043 (:1) XORL DX, DX
0x002d 00045 (:1) JMP 72
0x002f 00047 (:1) PCDATA $0, $0
0x002f 00047 (:1) MOVQ ""..autotmp_8+32(SP), BX
0x0034 00052 (:1) LEAQ 1(BX), DX
0x0038 00056 (:1) PCDATA $0, $3
0x0038 00056 (:1) MOVQ "".p+56(SP), BX
0x003d 00061 (:1) PCDATA $0, $4
0x003d 00061 (:1) MOVQ "".q+64(SP), SI
0x0042 00066 (:1) PCDATA $0, $5
0x0042 00066 (:1) MOVQ BX, AX
0x0045 00069 (:1) PCDATA $0, $2
0x0045 00069 (:1) MOVQ SI, CX
0x0048 00072 (:1) CMPQ DX, $2
0x004c 00076 (:1) JGE 153
0x004e 00078 (:1) MOVQ DX, BX
0x0051 00081 (:1) SHLQ $4, DX
0x0055 00085 (:1) MOVQ 8(AX)(DX*1), SI
0x005a 00090 (:1) PCDATA $0, $6
0x005a 00090 (:1) MOVQ (DX)(CX*1), DI
0x005e 00094 (:1) PCDATA $0, $7
0x005e 00094 (:1) MOVQ (DX)(AX*1), R8
0x0062 00098 (:1) PCDATA $0, $8
0x0062 00098 (:1) ADDQ CX, DX
0x0065 00101 (:1) PCDATA $0, $9
0x0065 00101 (:1) CMPQ 8(DX), SI
0x0069 00105 (:1) JNE 138
0x006b 00107 (:1) MOVQ BX, ""..autotmp_8+32(SP)
0x0070 00112 (:1) PCDATA $0, $10
0x0070 00112 (:1) MOVQ R8, (SP)
0x0074 00116 (:1) PCDATA $0, $0
0x0074 00116 (:1) MOVQ DI, 8(SP)
0x0079 00121 (:1) MOVQ SI, 16(SP)
0x007e 00126 (:1) CALL runtime.memequal(SB)
0x0083 00131 (:1) CMPB 24(SP), $0
0x0088 00136 (:1) JNE 47
0x008a 00138 (:1) PCDATA $1, $1
0x008a 00138 (:1) MOVB $0, "".~r2+72(SP)
0x008f 00143 (:1) MOVQ 40(SP), BP
0x0094 00148 (:1) ADDQ $48, SP
0x0098 00152 (:1) RET
0x0099 00153 (:1) MOVB $1, "".~r2+72(SP)
0x009e 00158 (:1) MOVQ 40(SP), BP
0x00a3 00163 (:1) ADDQ $48, SP
0x00a7 00167 (:1) RET
0x00a8 00168 (:1) NOP
0x00a8 00168 (:1) PCDATA $1, $-1
0x00a8 00168 (:1) PCDATA $0, $-2
0x00a8 00168 (:1) CALL runtime.morestack_noctxt(SB)
0x00ad 00173 (:1) PCDATA $0, $-1
0x00ad 00173 (:1) JMP 0
0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 0f 86 95 eH..%....H;a....
0x0010 00 00 00 48 83 ec 30 48 89 6c 24 28 48 8d 6c 24 ...H..0H.l$(H.l$
0x0020 28 48 8b 44 24 38 48 8b 4c 24 40 31 d2 eb 19 48 (H.D$8H.L$@1...H
0x0030 8b 5c 24 20 48 8d 53 01 48 8b 5c 24 38 48 8b 74 .$ H.S.H.$8H.t
0x0040 24 40 48 89 d8 48 89 f1 48 83 fa 02 7d 4b 48 89 $@H..H..H...}KH.
0x0050 d3 48 c1 e2 04 48 8b 74 10 08 48 8b 3c 0a 4c 8b .H...H.t..H.+0
rel 53+4 t=29 go.info.int+0
rel 57+4 t=29 go.loc.type..eq.[2]interface {}+0
rel 66+4 t=29 go.info.*[2]interface {}+0
rel 70+4 t=29 go.loc.type..eq.[2]interface {}+35
rel 79+4 t=29 go.info.*[2]interface {}+0
rel 83+4 t=29 go.loc.type..eq.[2]interface {}+70
rel 94+4 t=29 go.info.bool+0
go.range.type..eq.[2]interface {} SDWARFRANGE dupok size=0
go.debuglines.type..eq.[2]interface {} SDWARFMISC dupok size=29
0x0000 04 01 0f 0a cd 06 cd 06 08 73 06 37 06 02 27 ff .........s.7..'.
0x0010 06 41 06 73 06 41 06 73 04 01 03 00 01 .A.s.A.s.....
type..eqfunc.[2]interface {} SRODATA dupok size=8
0x0000 00 00 00 00 00 00 00 00 ........
rel 0+8 t=1 type..eq.[2]interface {}+0
type..namedata.*[2]interface {}- SRODATA dupok size=19
0x0000 00 00 10 2a 5b 32 5d 69 6e 74 65 72 66 61 63 65 ...*[2]interface
0x0010 20 7b 7d {}
type.*[2]interface {} SRODATA dupok size=56
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 be 73 2d 71 08 08 08 36 00 00 00 00 00 00 00 00 .s-q...6........
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 runtime.memequal64·f+0
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*[2]interface {}-+0
rel 48+8 t=1 type.[2]interface {}+0
runtime.gcbits.0a SRODATA dupok size=1
0x0000 0a .
type.[2]interface {} SRODATA dupok size=72
0x0000 20 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 ....... .......
0x0010 2c 59 a4 f1 02 08 08 11 00 00 00 00 00 00 00 00 ,Y..............
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0040 02 00 00 00 00 00 00 00 ........
rel 24+8 t=1 type..eqfunc.[2]interface {}+0
rel 32+8 t=1 runtime.gcbits.0a+0
rel 40+4 t=5 type..namedata.*[2]interface {}-+0
rel 44+4 t=6 type.*[2]interface {}+0
rel 48+8 t=1 type.interface {}+0
rel 56+8 t=1 type.[]interface {}+0
go.loc."".BenchmarkAddStringWithJoin SDWARFLOC size=90
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 01 00 9c 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 02 00 91 48 00 00 00 00 00 00 00 00 00 ......H.........
0x0040 00 00 00 00 00 00 00 01 00 50 00 00 00 00 00 00 .........P......
0x0050 00 00 00 00 00 00 00 00 00 00 ..........
rel 0+8 t=53 "".BenchmarkAddStringWithJoin+0
rel 8+8 t=53 "".BenchmarkAddStringWithJoin+196
rel 35+8 t=53 "".BenchmarkAddStringWithJoin+42
rel 43+8 t=53 "".BenchmarkAddStringWithJoin+158
rel 55+8 t=53 "".BenchmarkAddStringWithJoin+158
rel 63+8 t=53 "".BenchmarkAddStringWithJoin+196
go.info."".BenchmarkAddStringWithJoin SDWARFINFO size=124
0x0000 03 22 22 2e 42 65 6e 63 68 6d 61 72 6b 41 64 64 ."".BenchmarkAdd
0x0010 53 74 72 69 6e 67 57 69 74 68 4a 6f 69 6e 00 00 StringWithJoin..
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................
0x0030 9c 00 00 00 00 01 0a 68 65 6c 6c 6f 00 1b 00 00 .......hello....
0x0040 00 00 00 0a 77 6f 72 6c 64 00 1c 00 00 00 00 00 ....world.......
0x0050 10 62 00 00 1a 00 00 00 00 00 00 00 00 15 00 00 .b..............
0x0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b 69 ...............i
0x0070 00 1d 00 00 00 00 00 00 00 00 00 00 ............
rel 0+0 t=24 type.*testing.B+0
rel 0+0 t=24 type.[2]string+0
rel 0+0 t=24 type.int+0
rel 31+8 t=1 "".BenchmarkAddStringWithJoin+0
rel 39+8 t=1 "".BenchmarkAddStringWithJoin+196
rel 49+4 t=30 gofile../Users/dashen/go/src/note/blog/join_test.go+0
rel 62+4 t=29 go.info.string+0
rel 75+4 t=29 go.info.string+0
rel 85+4 t=29 go.info.*testing.B+0
rel 89+4 t=29 go.loc."".BenchmarkAddStringWithJoin+0
rel 94+8 t=1 "".BenchmarkAddStringWithJoin+35
rel 102+8 t=1 "".BenchmarkAddStringWithJoin+186
rel 114+4 t=29 go.info.int+0
rel 118+4 t=29 go.loc."".BenchmarkAddStringWithJoin+35
go.range."".BenchmarkAddStringWithJoin SDWARFRANGE size=0
go.debuglines."".BenchmarkAddStringWithJoin SDWARFMISC size=35
0x0000 04 02 03 14 14 0a cd 06 9b 06 26 06 23 06 42 06 ..........&.#.B.
0x0010 2d 06 02 51 fe 06 41 06 2d 06 41 06 91 70 04 01 -..Q..A.-.A..p..
0x0020 03 67 01 .g.
type..namedata.*[]string- SRODATA dupok size=12
0x0000 00 00 09 2a 5b 5d 73 74 72 69 6e 67 ...*[]string
type.*[]string SRODATA dupok size=56
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 92 22 76 84 08 08 08 36 00 00 00 00 00 00 00 00 ."v....6........
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 runtime.memequal64·f+0
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*[]string-+0
rel 48+8 t=1 type.[]string+0
type.[]string SRODATA dupok size=56
0x0000 18 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 d3 a8 f3 0a 02 08 08 17 00 00 00 00 00 00 00 00 ................
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 ........
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*[]string-+0
rel 44+4 t=6 type.*[]string+0
rel 48+8 t=1 type.string+0
go.loc.type..eq.[2]string SDWARFLOC dupok size=106
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 01 00 51 00 00 00 00 00 00 00 00 00 00 00 00 00 ..Q.............
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 01 00 9c 00 00 00 00 00 00 00 00 00 00 ................
0x0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0050 00 00 00 00 00 00 02 00 91 08 00 00 00 00 00 00 ................
0x0060 00 00 00 00 00 00 00 00 00 00 ..........
rel 0+8 t=53 type..eq.[2]string+72
rel 8+8 t=53 type..eq.[2]string+85
rel 35+8 t=53 type..eq.[2]string+0
rel 43+8 t=53 type..eq.[2]string+178
rel 70+8 t=53 type..eq.[2]string+0
rel 78+8 t=53 type..eq.[2]string+178
go.info.type..eq.[2]string SDWARFINFO dupok size=94
0x0000 03 74 79 70 65 2e 2e 65 71 2e 5b 32 5d 73 74 72 .type..eq.[2]str
0x0010 69 6e 67 00 00 00 00 00 00 00 00 00 00 00 00 00 ing.............
0x0020 00 00 00 00 01 9c 00 00 00 00 01 0b 69 00 01 00 ............i...
0x0030 00 00 00 00 00 00 00 10 70 00 00 01 00 00 00 00 ........p.......
0x0040 00 00 00 00 10 71 00 00 01 00 00 00 00 00 00 00 .....q..........
0x0050 00 0f 7e 72 32 00 01 01 00 00 00 00 00 00 ..~r2.........
rel 0+0 t=24 type.*[2]string+0
rel 0+0 t=24 type.bool+0
rel 0+0 t=24 type.int+0
rel 20+8 t=1 type..eq.[2]string+0
rel 28+8 t=1 type..eq.[2]string+178
rel 38+4 t=30 gofile..+0
rel 47+4 t=29 go.info.int+0
rel 51+4 t=29 go.loc.type..eq.[2]string+0
rel 60+4 t=29 go.info.*[2]string+0
rel 64+4 t=29 go.loc.type..eq.[2]string+35
rel 73+4 t=29 go.info.*[2]string+0
rel 77+4 t=29 go.loc.type..eq.[2]string+70
rel 88+4 t=29 go.info.bool+0
go.range.type..eq.[2]string SDWARFRANGE dupok size=0
go.debuglines.type..eq.[2]string SDWARFMISC dupok size=29
0x0000 04 01 0f 0a cd 06 cd 06 08 73 06 37 06 02 26 ff .........s.7..&.
0x0010 06 41 06 73 06 41 06 73 04 01 03 00 01 .A.s.A.s.....
type..eqfunc.[2]string SRODATA dupok size=8
0x0000 00 00 00 00 00 00 00 00 ........
rel 0+8 t=1 type..eq.[2]string+0
type..namedata.*[2]string- SRODATA dupok size=13
0x0000 00 00 0a 2a 5b 32 5d 73 74 72 69 6e 67 ...*[2]string
type.*[2]string SRODATA dupok size=56
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 0c 66 1f 3c 08 08 08 36 00 00 00 00 00 00 00 00 .f." SRODATA dupok size=5
0x0000 3c 6e 69 6c 3e
go.loc."".BenchmarkAddStringWithBuffer SDWARFLOC size=173
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 03 00 91 a0 7f 00 00 00 00 00 00 00 00 00 00 00 ................
0x0020 00 00 00 00 00 01 00 50 00 00 00 00 00 00 00 00 .......P........
0x0030 00 00 00 00 00 00 00 00 03 00 91 a0 7f 00 00 00 ................
0x0040 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 50 ...............P
0x0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0060 03 00 91 a0 7f 00 00 00 00 00 00 00 00 00 00 00 ................
0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0080 00 00 00 00 00 02 00 91 48 00 00 00 00 00 00 00 ........H.......
0x0090 00 00 00 00 00 00 00 00 00 02 00 91 48 00 00 00 ............H...
0x00a0 00 00 00 00 00 00 00 00 00 00 00 00 00 .............
rel 0+8 t=53 "".BenchmarkAddStringWithBuffer+107
rel 8+8 t=53 "".BenchmarkAddStringWithBuffer+110
rel 21+8 t=53 "".BenchmarkAddStringWithBuffer+110
rel 29+8 t=53 "".BenchmarkAddStringWithBuffer+154
rel 40+8 t=53 "".BenchmarkAddStringWithBuffer+154
rel 48+8 t=53 "".BenchmarkAddStringWithBuffer+286
rel 61+8 t=53 "".BenchmarkAddStringWithBuffer+286
rel 69+8 t=53 "".BenchmarkAddStringWithBuffer+302
rel 80+8 t=53 "".BenchmarkAddStringWithBuffer+302
rel 88+8 t=53 "".BenchmarkAddStringWithBuffer+321
rel 117+8 t=53 "".BenchmarkAddStringWithBuffer+107
rel 125+8 t=53 "".BenchmarkAddStringWithBuffer+286
rel 137+8 t=53 "".BenchmarkAddStringWithBuffer+302
rel 145+8 t=53 "".BenchmarkAddStringWithBuffer+321
go.info."".BenchmarkAddStringWithBuffer SDWARFINFO size=164
0x0000 03 22 22 2e 42 65 6e 63 68 6d 61 72 6b 41 64 64 ."".BenchmarkAdd
0x0010 53 74 72 69 6e 67 57 69 74 68 42 75 66 66 65 72 StringWithBuffer
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 01 9c 00 00 00 00 01 0a 68 65 6c 6c 6f 00 23 .........hello.#
0x0040 00 00 00 00 00 0a 77 6f 72 6c 64 00 24 00 00 00 ......world.$...
0x0050 00 00 0f 62 00 00 22 00 00 00 00 00 14 00 00 00 ...b..".........
0x0060 00 0b 69 00 25 00 00 00 00 00 00 00 00 15 00 00 ..i.%...........
0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b 62 ...............b
0x0080 75 66 66 65 72 00 26 00 00 00 00 00 00 00 00 00 uffer.&.........
0x0090 00 07 00 00 00 00 00 00 00 00 00 00 00 00 2a 00 ..............*.
0x00a0 00 00 00 00 ....
rel 0+0 t=24 type.*testing.B+0
rel 0+0 t=24 type.[32]uint8+0
rel 0+0 t=24 type.bytes.Buffer+0
rel 0+0 t=24 type.int+0
rel 33+8 t=1 "".BenchmarkAddStringWithBuffer+0
rel 41+8 t=1 "".BenchmarkAddStringWithBuffer+321
rel 51+4 t=30 gofile../Users/dashen/go/src/note/blog/join_test.go+0
rel 64+4 t=29 go.info.string+0
rel 77+4 t=29 go.info.string+0
rel 87+4 t=29 go.info.*testing.B+0
rel 93+4 t=29 go.range."".BenchmarkAddStringWithBuffer+0
rel 101+4 t=29 go.info.int+0
rel 105+4 t=29 go.loc."".BenchmarkAddStringWithBuffer+0
rel 110+8 t=1 "".BenchmarkAddStringWithBuffer+127
rel 118+8 t=1 "".BenchmarkAddStringWithBuffer+255
rel 135+4 t=29 go.info.bytes.Buffer+0
rel 139+4 t=29 go.loc."".BenchmarkAddStringWithBuffer+117
rel 146+4 t=29 go.info.bytes.(*Buffer).String$abstract+0
rel 150+4 t=29 go.range."".BenchmarkAddStringWithBuffer+64
rel 154+4 t=30 gofile../Users/dashen/go/src/note/blog/join_test.go+0
go.range."".BenchmarkAddStringWithBuffer SDWARFRANGE size=128
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
rel 0+8 t=53 "".BenchmarkAddStringWithBuffer+49
rel 8+8 t=53 "".BenchmarkAddStringWithBuffer+51
rel 16+8 t=53 "".BenchmarkAddStringWithBuffer+102
rel 24+8 t=53 "".BenchmarkAddStringWithBuffer+255
rel 32+8 t=53 "".BenchmarkAddStringWithBuffer+286
rel 40+8 t=53 "".BenchmarkAddStringWithBuffer+302
rel 64+8 t=53 "".BenchmarkAddStringWithBuffer+51
rel 72+8 t=53 "".BenchmarkAddStringWithBuffer+102
rel 80+8 t=53 "".BenchmarkAddStringWithBuffer+255
rel 88+8 t=53 "".BenchmarkAddStringWithBuffer+286
rel 96+8 t=53 "".BenchmarkAddStringWithBuffer+302
rel 104+8 t=53 "".BenchmarkAddStringWithBuffer+311
go.debuglines."".BenchmarkAddStringWithBuffer SDWARFMISC size=85
0x0000 04 02 03 1c 14 0a ff 06 f5 26 04 14 03 17 28 04 .........&....(.
0x0010 02 06 02 1b 03 68 fb 06 41 06 2d 06 4b 06 7e 06 .....h..A.-.K.~.
0x0020 69 06 92 06 41 06 08 19 42 06 41 06 08 19 42 06 i...A...B.A...B.
0x0030 41 06 08 19 42 04 14 03 12 1e 06 41 04 02 06 08 A...B......A....
0x0040 03 68 1f 04 14 06 03 17 b4 06 5f 04 02 03 65 15 .h........_...e.
0x0050 04 01 03 5f 01 ..._.
""..inittask SNOPTRDATA size=56
0x0000 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ................
0x0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 bytes..inittask+0
rel 32+8 t=1 fmt..inittask+0
rel 40+8 t=1 strings..inittask+0
rel 48+8 t=1 testing..inittask+0
type..namedata.*[]uint8- SRODATA dupok size=11
0x0000 00 00 08 2a 5b 5d 75 69 6e 74 38 ...*[]uint8
type.*[]uint8 SRODATA dupok size=56
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 a5 8e d0 69 08 08 08 36 00 00 00 00 00 00 00 00 ...i...6........
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 runtime.memequal64·f+0
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*[]uint8-+0
rel 48+8 t=1 type.[]uint8+0
type.[]uint8 SRODATA dupok size=56
0x0000 18 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 df 7e 2e 38 02 08 08 17 00 00 00 00 00 00 00 00 .~.8............
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 ........
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*[]uint8-+0
rel 44+4 t=6 type.*[]uint8+0
rel 48+8 t=1 type.uint8+0
type..eqfunc32 SRODATA dupok size=16
0x0000 00 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 ........ .......
rel 0+8 t=1 runtime.memequal_varlen+0
type..namedata.*[32]uint8- SRODATA dupok size=13
0x0000 00 00 0a 2a 5b 33 32 5d 75 69 6e 74 38 ...*[32]uint8
type.*[32]uint8 SRODATA dupok size=56
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 f4 c7 79 15 08 08 08 36 00 00 00 00 00 00 00 00 ..y....6........
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 runtime.memequal64·f+0
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*[32]uint8-+0
rel 48+8 t=1 type.[32]uint8+0
runtime.gcbits. SRODATA dupok size=0
type.[32]uint8 SRODATA dupok size=72
0x0000 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...............
0x0010 9c 59 ff a8 0a 01 01 11 00 00 00 00 00 00 00 00 .Y..............
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0040 20 00 00 00 00 00 00 00 .......
rel 24+8 t=1 type..eqfunc32+0
rel 32+8 t=1 runtime.gcbits.+0
rel 40+4 t=5 type..namedata.*[32]uint8-+0
rel 44+4 t=6 type.*[32]uint8+0
rel 48+8 t=1 type.uint8+0
rel 56+8 t=1 type.[]uint8+0
type..importpath.bytes. SRODATA dupok size=8
0x0000 00 00 05 62 79 74 65 73 ...bytes
type..importpath.fmt. SRODATA dupok size=6
0x0000 00 00 03 66 6d 74 ...fmt
type..importpath.strings. SRODATA dupok size=10
0x0000 00 00 07 73 74 72 69 6e 67 73 ...strings
type..importpath.testing. SRODATA dupok size=10
0x0000 00 00 07 74 65 73 74 69 6e 67 ...testing
gclocals·a36216b97439c93dafebe03e7f0808b5 SRODATA dupok size=9
0x0000 01 00 00 00 01 00 00 00 01 .........
gclocals·33cdeccccebe80329f1fdbee7f5874cb SRODATA dupok size=8
0x0000 01 00 00 00 00 00 00 00 ........
gclocals·96839595c383af6ae8227769d90a999e SRODATA dupok size=12
0x0000 04 00 00 00 03 00 00 00 00 01 02 04 ............
gclocals·69a9291448fa273f79569cb593f615b2 SRODATA dupok size=12
0x0000 04 00 00 00 01 00 00 00 01 01 01 01 ............
gclocals·306ea4dc593c4841cf74b8270a1a332d SRODATA dupok size=12
0x0000 04 00 00 00 05 00 00 00 00 01 15 14 ............
gclocals·a5a3f3e663b1ae9c2739369df7a1eae5 SRODATA dupok size=14
0x0000 06 00 00 00 03 00 00 00 00 01 02 03 07 04 ..............
"".BenchmarkAddStringWithSprintf.stkobj SRODATA size=24
0x0000 01 00 00 00 00 00 00 00 e0 ff ff ff ff ff ff ff ................
0x0010 00 00 00 00 00 00 00 00 ........
rel 16+8 t=1 type.[2]interface {}+0
gclocals·dc9b0298814590ca3ffc3a889546fc8b SRODATA dupok size=10
0x0000 02 00 00 00 02 00 00 00 03 00 ..........
gclocals·69c1753bd5f81501d95132d08af04464 SRODATA dupok size=8
0x0000 02 00 00 00 00 00 00 00 ........
gclocals·313a5bdbfadc4f007c002a3a3588596d SRODATA dupok size=18
0x0000 0a 00 00 00 06 00 00 00 00 01 03 08 28 21 23 22 ............(!#"
0x0010 24 04 $.
gclocals·09b80ec389a9e6ac09cfa1cd3c45263d SRODATA dupok size=10
0x0000 02 00 00 00 01 00 00 00 01 01 ..........
gclocals·fb107d7ecaf558140670273b9a8c1e62 SRODATA dupok size=10
0x0000 02 00 00 00 04 00 00 00 00 05 ..........
"".BenchmarkAddStringWithJoin.stkobj SRODATA size=24
0x0000 01 00 00 00 00 00 00 00 e0 ff ff ff ff ff ff ff ................
0x0010 00 00 00 00 00 00 00 00 ........
rel 16+8 t=1 type.[2]string+0
gclocals·8a866e72a2964e4d3575e01f98e6fe5f SRODATA dupok size=19
0x0000 0b 00 00 00 08 00 00 00 00 01 03 08 28 21 43 c2 ............(!C.
0x0010 c4 c0 40 ..@
gclocals·263043c8f03e3241528dfae4e2812ef4 SRODATA dupok size=10
0x0000 02 00 00 00 01 00 00 00 00 00 ..........
gclocals·d527b79a98f329c2ba624a68e7df03d6 SRODATA dupok size=10
0x0000 02 00 00 00 05 00 00 00 00 01 ..........
gclocals·477643caa859a1fb5c2b872c45af6067 SRODATA dupok size=12
0x0000 04 00 00 00 06 00 00 00 00 21 01 02 .........!..
"".BenchmarkAddStringWithBuffer.stkobj SRODATA size=24
0x0000 01 00 00 00 00 00 00 00 d8 ff ff ff ff ff ff ff ................
0x0010 00 00 00 00 00 00 00 00 ........
rel 16+8 t=1 type.bytes.Buffer+0
更多参考:
golang 几种字符串的拼接方式
后面给Go提交了几次代码,go/ast: use strings.Builder, go/parser: use strings.Builder,了解到bytes.Buffer是有内存分配的,推荐使用strings.Builder, 仔细审视了之前的代码,发现benchmark里for i := 0; i 这一行有问题,修改并新增strings.Builder后的压测文件如下:
package shuang
import (
"bytes"
"fmt"
"strings"
"testing"
)
func BenchmarkAddStringWithOperator(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i
go test -test.bench=".*" -benchmem
:
goos: darwin
goarch: arm64
pkg: shuang/bc2
BenchmarkAddStringWithOperator-8 73952530 15.59 ns/op 0 B/op 0 allocs/op
BenchmarkAddStringWithSprintf-8 12973456 91.93 ns/op 48 B/op 3 allocs/op
BenchmarkAddStringWithJoin-8 44449520 26.82 ns/op 16 B/op 1 allocs/op
BenchmarkAddStringWithBuffer-8 36167272 32.81 ns/op 64 B/op 1 allocs/op
BenchmarkAddStringWithBuilder-8 36218533 32.60 ns/op 24 B/op 2 allocs/op
PASS
ok shuang/bc2 6.839s
Go 1.9 版本后,strings包新增加strings.Builder
看起来strings.Builder
相比于bytes.Buffer
会多分配一次内存,但单次内存分配的大小小于bytes.Buffer
strings.Join
底层其实也是调用strings.Builder
,为什么前者在每次执行耗时及每次内存分配大小上,优于后者? 有空时仔细研究下~
以上都只是多次拼接hello,world。还有一种一直追加(append而非add)的拼接,即循环里不断将新字符串追加到原字符串之后:
如将字符串cuishuang, ,append形式拼接10000次
package shuang
import (
"bytes"
"fmt"
"strings"
"testing"
)
func BenchmarkAddStringWithOperator(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i
go test -test.bench=".*" -benchmem
:
goos: darwin
goarch: arm64
pkg: shuang/bc2
BenchmarkAddStringWithOperator-8 76988056 15.62 ns/op 0 B/op 0 allocs/op
BenchmarkAddStringWithSprintf-8 12866815 97.66 ns/op 48 B/op 3 allocs/op
BenchmarkAddStringWithJoin-8 43484365 27.59 ns/op 16 B/op 1 allocs/op
BenchmarkAddStringWithBuffer-8 35710210 32.96 ns/op 64 B/op 1 allocs/op
BenchmarkAddStringWithBuilder-8 34601161 33.51 ns/op 24 B/op 2 allocs/op
BenchmarkAppendWithAdd-8 317286 95134 ns/op 1590508 B/op 1 allocs/op
BenchmarkAppendWithSprintf-8 95326 84243 ns/op 959952 B/op 3 allocs/op
BenchmarkAppendWithJoin-8 272533 100585 ns/op 1366737 B/op 1 allocs/op
BenchmarkAppendWithBytesBuffer-8 100000000 12.74 ns/op 31 B/op 0 allocs/op
BenchmarkAppendWithStringBuilder-8 100000000 10.83 ns/op 57 B/op 0 allocs/op
PASS
ok shuang/bc2 75.146s
由于string是不可修改的,所以在使用“+”进行拼接字符串,每次都会产生申请空间,拼接,复制等操作,数据量大的情况下非常消耗资源和性能。而采用Buffer等方式,都是预先计算拼接字符串数组的总长度(如果可以知道长度),申请空间,底层是slice数组,可以以append的形式向后进行追加。最后在转换为字符串。这申请了不断申请空间的操作,也减少了空间的使用和拷贝的次数,自然性能也高不少
go语言string之Buffer与Builder
一般情况下strings.Builder
性能略好于bytes.Buffer
其中一个原因是bytes.Buffer
最后将byte切片转为string的String()方法,就是将字节切片强转为string(强转的时候是需要进行申请空间,并拷贝的)
// To build strings more efficiently, see the strings.Builder type.
func (b *Buffer) String() string {
if b == nil {
// Special case, useful in debugging.
return ""
}
return string(b.buf[b.off:])
}
而 strings.Builder
使用*(*string)(unsafe.Pointer(&b.buf))
,以unsafe.Pointer媒介,程序绕过类型系统,进行地址转换而不是拷贝
// String returns the accumulated string.
func (b *Builder) String() string {
return *(*string)(unsafe.Pointer(&b.buf))
}
即 在最后由字节切片转为string时, bytes.Buffer
重新申请了一块空间,存放生成的string变量, 而strings.Builder
通过*(*string)(unsafe.Pointer(&byteSli))
直接将底层的[]byte转换成了string类型返回了回来,省掉了申请空间的操作
参考下面 byte切片转string
更多参考:
strings.Builder 转换字符串的时候为什么比 bytes.Buffer 要快
go strings.Builder和bytes.Buffer
Go bytes.Buffer 和 strings.Builder 性能比较
byte切片转string
package main
import "fmt"
func main() {
str := `{"default":{"common":{"pet":{"five":"斑斑","four":"皮瓜瓜","one":"弥弥懵","three":"呆呆","two":"黄橙橙"},"relation":{"father":"cuixxxxxxx","mother":"yinxxxxx","wife":"pengxx"}}}}`
fmt.Println([]byte(str))
}
先得到 byte类型的切片
输出:
[123 34 100 101 102 97 117 108 116 34 58 123 34 99 111 109 109 111 110 34 58 123 34 112 101 116 34 58 123 34 102 105 118 101 34 58 34 230 150 145 230 150 145 34 44 34 102 111 117 114 34 58 34 231 154 174 231 147 156 231 147 156 34 44 34 111 110 101 34 58 34 229 188 165 229 188 165 230 135 181 34 44 34 116 104 114 101 101 34 58 34 229 145 134 229 145 134 34 44 34 116 119 111 34 58 34 233 187 132 230 169 153 230 169 153 34 125 44 34 114 101 108 97 116 105 111 110 34 58 123 34 102 97 116 104 101 114 34 58 34 99 117 105 120 120 120 120 120 120 120 34 44 34 109 111 116 104 101 114 34 58 34 121 105 110 120 120 120 120 120 34 44 34 119 105 102 101 34 58 34 112 101 110 103 120 120 34 125 125 125 125]
byteSliToStr.go:
package main
import (
"testing"
"unsafe"
)
/**
原始字符串
`{"default":{"common":{"pet":{"five":"斑斑","four":"皮瓜瓜","one":"弥弥懵","three":"呆呆","two":"黄橙橙"},"relation":{"father":"cuixxxxxxx","mother":"yinxxxxx","wife":"pengxx"}}}}`
*/
func BenchmarkString(b *testing.B) {
byteSli := []byte{123, 34, 100, 101, 102, 97, 117, 108, 116, 34, 58, 123, 34, 99, 111, 109, 109, 111, 110, 34, 58, 123, 34, 112, 101, 116, 34, 58, 123, 34, 102, 105, 118, 101, 34, 58, 34, 230, 150, 145, 230, 150, 145, 34, 44, 34, 102, 111, 117, 114, 34, 58, 34, 231, 154, 174, 231, 147, 156, 231, 147, 156, 34, 44, 34, 111, 110, 101, 34, 58, 34, 229, 188, 165, 229, 188, 165, 230, 135, 181, 34, 44, 34, 116, 104, 114, 101, 101, 34, 58, 34, 229, 145, 134, 229, 145, 134, 34, 44, 34, 116, 119, 111, 34, 58, 34, 233, 187, 132, 230, 169, 153, 230, 169, 153, 34, 125, 44, 34, 114, 101, 108, 97, 116, 105, 111, 110, 34, 58, 123, 34, 102, 97, 116, 104, 101, 114, 34, 58, 34, 99, 117, 105, 120, 120, 120, 120, 120, 120, 120, 34, 44, 34, 109, 111, 116, 104, 101, 114, 34, 58, 34, 121, 105, 110, 120, 120, 120, 120, 120, 34, 44, 34, 119, 105, 102, 101, 34, 58, 34, 112, 101, 110, 103, 120, 120, 34, 125, 125, 125, 125}
_ = string(byteSli)
}
func BenchmarkUnsafe(b *testing.B) {
byteSli := []byte{123, 34, 100, 101, 102, 97, 117, 108, 116, 34, 58, 123, 34, 99, 111, 109, 109, 111, 110, 34, 58, 123, 34, 112, 101, 116, 34, 58, 123, 34, 102, 105, 118, 101, 34, 58, 34, 230, 150, 145, 230, 150, 145, 34, 44, 34, 102, 111, 117, 114, 34, 58, 34, 231, 154, 174, 231, 147, 156, 231, 147, 156, 34, 44, 34, 111, 110, 101, 34, 58, 34, 229, 188, 165, 229, 188, 165, 230, 135, 181, 34, 44, 34, 116, 104, 114, 101, 101, 34, 58, 34, 229, 145, 134, 229, 145, 134, 34, 44, 34, 116, 119, 111, 34, 58, 34, 233, 187, 132, 230, 169, 153, 230, 169, 153, 34, 125, 44, 34, 114, 101, 108, 97, 116, 105, 111, 110, 34, 58, 123, 34, 102, 97, 116, 104, 101, 114, 34, 58, 34, 99, 117, 105, 120, 120, 120, 120, 120, 120, 120, 34, 44, 34, 109, 111, 116, 104, 101, 114, 34, 58, 34, 121, 105, 110, 120, 120, 120, 120, 120, 34, 44, 34, 119, 105, 102, 101, 34, 58, 34, 112, 101, 110, 103, 120, 120, 34, 125, 125, 125, 125}
_ = *(*string)(unsafe.Pointer(&byteSli))
}
bench_test.go:
package main
import (
"testing"
)
func BenchmarkTest1(b *testing.B) {
for i := 0; i
执行 go test -test.bench=".*" -benchmem
:
goos: darwin
goarch: arm64
pkg: xxxx
BenchmarkTest1-8 16376076 61.51 ns/op 192 B/op 1 allocs/op
BenchmarkTest2-8 34398655 33.49 ns/op 0 B/op 0 allocs/op
PASS
ok xxxx 2.363s
第4行显示了BenchmarkString 执行了16376076次,每次的执行平均时间是61.51纳秒, 每次操作有1次内存分配,每次分配了192Byte大小的内存空间
第5行显示了BenchmarkUnsafe 执行了34398655次,每次的平均执行时间是33.49 纳秒, 无内存分配
可见使用unsafe这种”黑科技”,确实可以少分配一次内存
也可看出,string(byteSli)的方式是深拷贝,为新生成的新字符串新分配了一块内存
string转byte切片
再看一下上面的逆操作
strToByteSli.go:
package main
import (
"reflect"
"testing"
"unsafe"
)
func BenchmarkByteStyle(b *testing.B) {
str := `{"default":{"common":{"pet":{"five":"斑斑","four":"皮瓜瓜","one":"弥弥懵","three":"呆呆","two":"黄橙橙"},"relation":{"father":"cuixxxxxxx","mother":"yinxxxxx","wife":"pengxx"}}}}`
_ = []byte(str)
}
func BenchmarkWithUnsafe(b *testing.B) {
str := `{"default":{"common":{"pet":{"five":"斑斑","four":"皮瓜瓜","one":"弥弥懵","three":"呆呆","two":"黄橙橙"},"relation":{"father":"cuixxxxxxx","mother":"yinxxxxx","wife":"pengxx"}}}}`
sh := (*reflect.StringHeader)(unsafe.Pointer(&str))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
_ = *(*[]byte)(unsafe.Pointer(&bh))
}
bench_test.go:
package main
import (
"testing"
)
func BenchmarkTest3(b *testing.B) {
for i := 0; i
执行 go test -test.bench=".*" -benchmem
:
goos: darwin
goarch: arm64
pkg: xxxx
BenchmarkTest3-8 34892566 34.03 ns/op 192 B/op 1 allocs/op
BenchmarkTest4-8 1000000000 0.3148 ns/op 0 B/op 0 allocs/op
PASS
ok xxxx 2.873s
第4行显示了BenchmarkByteStyle 执行了34892566次,每次的执行平均时间是34.03纳秒, 每次操作有1次内存分配,每次分配了192Byte大小的内存空间
第5行显示了BenchmarkWithUnsafe 执行了1000000000次,每次的平均执行时间是0.3148纳秒, 无内存分配
使用unsafe不仅可以少分配一次内存,每次的平均执行时间也差了100倍…(而用unsafe 从[]byte到string,和使用string(byteSli)方式,执行时间只快了一倍)
[string和[]byte转换会发生内存拷贝吗](https://dashen.tech/2012/04/03/string%E5%92%8C-byte%E8%BD%AC%…)
本文由mdnice多平台发布
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net