With recycling Without recycling name time/op name time/op PrimeNumber 12.7s ± 3% PrimeNumber 12.1s ± 3% PrimeNumber-8 2.27s ± 4% PrimeNumber-8 2.13s ± 3%
name alloc/op name alloc/op PrimeNumber 1.83MB ± 0% PrimeNumber 5.82MB ± 4% PrimeNumber-8 1.52MB ± 7% PrimeNumber-8 5.90MB ±21%
没有关闭协程回收的方法,作者通过直接修改go标准库来测试的。可以看见,协程回收减少了3M内存分配。
让我们再来看另一个使用大栈空间的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
func ping() { for i := 0; i < 10; i++ { var wg sync.WaitGroup for g := 0; g < 10; g++ { wg.Add(1) go func() { _, err := http.Get(`https://httpstat.us/200`) if err != nil { panic(err) } wg.Done() }() } wg.Wait() } }
压测结果如下:
1 2 3 4 5 6 7 8
With recycling Without recycling name time/op name time/op PingUrl 12.8s ± 2% PingUrl 12.8s ± 3% PingUrl-8 12.6s ± 0% PingUrl-8 12.7s ± 3%
name alloc/op name alloc/op PingUrl 9.21MB ± 0% PingUrl 9.44MB ± 0% PingUrl-8 9.28MB ± 0% PingUrl-8 9.43MB ± 0%