这是Go 的开发者之一 Dave Cheney 介绍的5个GoLang 优化措施:

清晰赋值类型

比如确认一个数不会超过 uint32 就不要使用int,下表是数值的范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

赋值:

1
var gocon uint32 = 2015

数值能用小的用小的,尽量让数值留在CPU cache,而不是速度更慢的内存里。

减少函数调用

函数调用都有 overhead(额外开销),比如保存调用栈,CPU切出。

Go 编译器通常会尝试进行内联,将小函数直接复制并编译。

如下面代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import "fmt"

func main() {
    fmt.Println(DoubleMax(2, 10))
}

func Max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func DoubleMax(a, b int) int {
    return 2 * Max(a, b)
}
m
1
go build -gcflags=-m inline.go

可以看到:

1
2
3
4
5
$ go build -gcflags=-m inline.go 
# command-line-arguments
./inline.go:9: can inline Max
./inline.go:17: inlining call to Max
./inline.go:6: main ... argument does not escape
Max
DoubleMax
1
2
3
4
5
6
7
func DoubleMax(a, b int) int {
    tmp := b
    if a > b {
        tmp = a
    }
    return 2 * tmp
}

尽量使用局部变量

下图是进程地址空间示意:

进程地址空间

stack 作用域是本地的(locals),在函数执行完之后会自动收回,CPU 控制,效率高;而 heap 则需要由程序来管理,效率低。

应该把不需要传出的参数尽量控制在函数内。

局部变量

int

局部变量

new

使用 Goroutine 协程

scheduler
  • chan 收发
  • go 语句调用函数
  • 阻塞的syscall
  • gc

第五个太高级了,不常用。

本文网址: https://golangnote.com/topic/83.html 转摘请注明来源