前言: \textcolor{Green}{前言:} 前言:
这个专栏就专门来记录一下寒假参加的第五期字节跳动训练营
从这个专栏里面可以迅速获得Go的知识
性能优化对于一个系统来说是非常重要的,因为我们在编写代码结束的时候有可能不会对性能有过多的关注,但是对于用户来说性能是非常重要的,我们肯定希望使用的系统是流畅的。这是一个速度决定一切的年代,只要我们的还继续在这个时代中,线下的流程与系统就在持续向线上转移,我们就会碰到性能问题。
简介
如何使用
go test -bench=. -benchmen
// from fib.go
func Fib(n int) int {
if n < 2 {
return n
}
return Fib(n - 1) + Fib(n - 2)
}
// from fib_test.go
func BenchmarkFib10(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
Fib(10)
}
}
结果说明
GOMAXPROCS 1.5版本后,默认值为CPU核数,https://pkg.go.dev/runtime#GOMAXPROCS
slice 预分配内存
查看下面的代码,可以发现,提供容量信息后数据明显好
func NoPreAlloc(size int) {
data := make([]int, 0)
for k := 0; k < size; k++ {
data = append(data, k)
}
}
func PreAlloc(size int) {
data := make([]int, 0, size)
for k := 0; k < size; k++ {
data = append(data, k)
}
}
BenchmarkNoPreAlloc-8 | 3529980 | 331.1 ns/op | 2040 B/op | 8 allocs/op |
BenchmarkPreAlloc-8 | 11171086 | 107.1 ns/op | 896 B/op | 1 allocs/op |
type slice struct {
array unsafe.Pointer
len int
cap int
}
另一个陷阱:大内存未释放
func GetLastBySlice(origin []int) []int {
return origin[len(origin)-2:]
}
func GetLastByCopy(origin []int) []int {
result := make([]int, 2)
copy(result, origin[len(origin)-2:])
return result
}
func testGetLast(t *testing.T, f func([]int) []int) {
result := make([][]int, 0)
for k := 0; k < 100; k++ {
origin := generateWithCap(128 * 1024) // 1M
result = append(result, f(origin))
}
printMem(t)
_ = result
}
可以查看链接直达
map 预分配内存
func NoPreAlloc(size int) {
data := make(map[int]int)
for k := 0; k < size; k++ {
data[i] = 1
}
}
func PreAlloc(size int) {
data := make(map[int]int, size)
for k := 0; k < size; k++ {
data[i] = 1
}
}
BenchmarkNoPreAlloc-8 | 22699 | 51972ns/op | 82327B/op | 9 allocs/op |
BenchmarkPreAlloc-8 | 123418 | 9622ns/op | 40984B/op | 2 allocs/op |
分析:
func Plus(n int, str string) string {
s := ""
for i := 0; i < n; i++ {
s += str
}
}
func StrBuilder(n int, str string) string {
var bulider strings.Builder
for i := 0; i < n; i++ {
builder.WriteString(str)
}
return builder.String()
}
使用 strings.Builder
func ByteBuffer(n int, str string) string {
buf := new(bytes.Buffer)
for i := 0; i < n; i++ {
buf.WriteString(str)
}
return buf.String()
}
BenchmarkPlus-8 | 4318 | 280260ns/op | 3212595 B/op | 999 allocs/op |
BenchmarkStrBulider-8 | 269257 | 2392 ns/op | 26744 B/op | 15 allocs/op |
BenchmarkByteBuffer-8 | 209278 | 5699 ns/op | 25008 B/op | 9 allocs/op |
注意看下面的
// 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:])
}
// String returns the accumulated string
func (b *Builder) String() string {
return *(*string)(unsafe.Pointer(&b.buf))
}
func PreStrBuilder(n int, str string) string {
var bulider strings.Builder
builder.Grow(n * len(str))
for i := 0; i < n; i++ {
builder.WriteString(str)
}
return builder.String()
}
func PreByteBuilder(n int, str string) string {
buf := new(bytes.Buffer)
buf.Grow(n * len(str))
for i := 0; i < n; i++ {
buf .WriteString(str)
}
return buf .String()
}
这五个进行对比
BenchmarkPlus-8 | 4272 | 279704 ns/op | 3212596 B/op | 999 allocs/op |
BenchmarkStrBulider-8 | 268747 | 4405 ns/op | 26744 B/op | 15 allocs/op |
BenchmarkByteBuffer-8 | 4670 | 5670 ns/op | 25008 B/op | 9 allocs/op |
BenchmarkPreStrBulider-8 | 3938 | 3938 ns/op | 6144 B/op | 1 allocs/op |
BenchmarkPreByteBuffer-8 | 4578 | 4578 ns/op | 12288 B/op | 2 allocs/op |
使用空结构体节省内存
func EmptyStructMap(n int) {
m := make(map[int]struct{})
for i := 0; i < n; i++ {
m[i] = struct{}{}
}
}
func BoolMap(n int){
m := make(map[int]bool)
for i := 0; i < n; i++ {
m[i] = false
}
}
BenchmarkEmptyStructMap-8 | 2372 | 505970 ns/op | 378864 B/op | 133 allocs/op |
BenchmarkBoolMap-8 | 2266 | 526095 ns/op | 412362 B/op | 165 allocs/op |
一个开源实现:https://github.com/deckarep/golang-set/blob/main/threadunsafe.go
如何使用 atomic 包
type atomicCounter struct {
i int32
}
func AtomicAddOne(c *atomicCounter) {
atomic.AddInt32(&c, i, 1)
}
type mutexCounter struct {
i int32
m sync.Mutex
}
func MutexAddOne(c *mutexCounter) {
c.m.Lock()
c.i++
c.m.Unlock()
}
BenchmarkAtomicAddOne-8 | 141824372 | 8.045 ns/op | 4 B/op | 1 allocs/op |
BenchmarkNutexAddOne-8 | 60487044 | 21.73 ns/op | 16 B/op | 1 allocs/op |
使用 atomic 包