5个GoLang 应用优化措施

这是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

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

原文 Five things that make Go fast

转载于:https://my.oschina.net/wdyoschina/blog/710251

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值