5个GoLang 应用优化措施
🕚 Sat, 26 Sep 2015 by GolangNote
这是Go 的开发者之一 Dave Cheney 介绍的5个GoLang 优化措施:
清晰赋值类型
比如确认一个数不会超过 uint32 就不要使用int,下表是数值的范围
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
赋值:
var gocon uint32 = 2015
数值能用小的用小的,尽量让数值留在CPU cache,而不是速度更慢的内存里。
减少函数调用
函数调用都有 overhead(额外开销),比如保存调用栈,CPU切出。
Go 编译器通常会尝试进行内联,将小函数直接复制并编译。
如下面代码:
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 查看内联状态:
go build -gcflags=-m inline.go
可以看到:
$ 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 函数就是这个样子:
func DoubleMax(a, b int) int {
tmp := b
if a > b {
tmp = a
}
return 2 * tmp
}
尽量使用局部变量
下图是进程地址空间示意:
stack作用域是本地的(locals),在函数执行完之后会自动收回,CPU控制,效率高;而heap则需要由程序来管理,效率低。
应该把不需要传出的参数尽量控制在函数内。
上图的变量numbers 只在 Sum中,编译器也会自动分配100个int空间在stack中,而不是heap中。
上图变量c是通过new函数生成的,但是因为在center外没有c的引用,所以c也会被存储在stack上。
使用Goroutine 协程
Goroutine是比进程、线程都小的执行单元,Goroutine中的会被调度器(scheduler)切出的操作:
- chan 收发
- go 语句调用函数
- 阻塞的syscall
- gc
第五个太高级了,不常用。
原文 http://dave.cheney.net/2014/06/07/five-things-that-make-go-fast 51
本文网址: https://golangnote.com/topic/83.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Relative Articles
Recent Go Articles
- Golang 把cookie 字符串解析为cookie 结构
- Golang 计算字符串中包含某个或某些字符集的个数
- 使用Golang 对文件增删写读操作备忘
- Go Modules 使用备忘
- 使用Golang 简单删除图片exif 信息
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
Top Go Articles
- 使用Golang 简单删除图片exif 信息
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
- Golang get 请求忽略数字证书进行校验
- Golang字符串相等比较的性能
- an error broken: unknown escape sequence: d
- golang 正确获取绝对路径的方法