Go:缓冲通道的求和更快吗?

huangapple go评论189阅读模式
英文:

Go : buffered channel sum is faster?

问题

Go:缓冲通道的求和更快吗?

在下面的playground链接中,

http://play.golang.org/p/8OAbtn6koN

有一个包含缓冲通道的方法,我期望它的性能更好,因为它不需要等待同步。

 ch := make(chan int, 2)

有三种方法对随机数数组进行求和。我使用以下链接对这三个函数进行基准测试:

http://play.golang.org/p/JK3yL4QwOJ

我期望缓冲通道是异步的,发送或接收不需要等待,除非通道已满,所以缓冲通道的求和比非缓冲通道的求和性能更好,因为它不需要时间来同步所有的goroutine。

[问题1]
所以我给缓冲通道求和函数设置了3个缓冲区大小,但基准测试结果如下:

Benchmarklinear       10   122170091 ns/op
BenchmarkchSum        20   121921287 ns/op
testing: BenchmarkchSum left GOMAXPROCS set to 4
BenchmarkchSumBuffer        20   118524619 ns/op
testing: BenchmarkchSumBuffer left GOMAXPROCS set to 4
ok    ~/Benchmark_sum_01  4.056s

这没有意义,为什么在这种情况下两个通道求和的性能相同?带有缓冲区的通道求和不应该性能更好吗?

[问题2]
如果我设置

 ch := make(chan int, 1)

我得到

Benchmarklinear       10   122170091 ns/op
BenchmarkchSum        20   121921287 ns/op
testing: BenchmarkchSum left GOMAXPROCS set to 4
BenchmarkchSumBuffer        10   118524619 ns/op
testing: BenchmarkchSumBuffer left GOMAXPROCS set to 4

如果我设置

 ch := make(chan int, 2)

我得到

Benchmarklinear       10   122170091 ns/op
BenchmarkchSum        10   121921287 ns/op
testing: BenchmarkchSum left GOMAXPROCS set to 4
BenchmarkchSumBuffer        20   118524619 ns/op
testing: BenchmarkchSumBuffer left GOMAXPROCS set to 4

但结果非常不一致。每次运行基准测试,我都会得到不同的结果。

缓冲区大小与性能无关吗?为什么缓冲区大小为2的基准测试给出了不同的结果?使用更大的缓冲区,两个goroutine永远不会阻塞,所以看起来需要更少的时间。

提前感谢。

英文:

Go : buffered channel sum is faster?

On the following playground link,

http://play.golang.org/p/8OAbtn6koN

One method contains buffered channel, and I expected this to perform better because it does not wait for synchronization.

 ch := make(chan int, 2)

Three methods that sums an array of random numbers. I benchmark these three functions like the following link:

http://play.golang.org/p/JK3yL4QwOJ

My expection was Buffered channel is Asynchronous and sending or receiving need not wait unless the channel is full, so the buffured channel sum performs better than unbuffered channel sum because it does not take any time to synchronize all goroutines.

[Question 1]
So I gave 3 buffer size for buffered channel sum function but the benchmark result is like the following:

Benchmarklinear       10   122170091 ns/op
BenchmarkchSum        20   121921287 ns/op
testing: BenchmarkchSum left GOMAXPROCS set to 4
BenchmarkchSumBuffer        20   118524619 ns/op
testing: BenchmarkchSumBuffer left GOMAXPROCS set to 4
ok    ~/Benchmark_sum_01  4.056s

It does not make sense, why in this case the two channel sum perform the same? Should the one with buffer perform better?

[Question 2]

If I give

 ch := make(chan int, 1)

I get

Benchmarklinear       10   122170091 ns/op
BenchmarkchSum        20   121921287 ns/op
testing: BenchmarkchSum left GOMAXPROCS set to 4
BenchmarkchSumBuffer        10   118524619 ns/op
testing: BenchmarkchSumBuffer left GOMAXPROCS set to 4

If I give

 ch := make(chan int, 2)

I get

Benchmarklinear       10   122170091 ns/op
BenchmarkchSum        10   121921287 ns/op
testing: BenchmarkchSum left GOMAXPROCS set to 4
BenchmarkchSumBuffer        20   118524619 ns/op
testing: BenchmarkchSumBuffer left GOMAXPROCS set to 4

But the result is very inconsistent. Everytime I run the benchmark test, I get a different result.

buffer size does not relate to the performance? Why the benchmark with buffersize 2 gives me different results. With more buffer size, two goroutines never blocks so it seems to take less time.

Thanks in advance.

答案1

得分: 0

缓冲区的缺失是无关紧要的。在无缓冲通道上有一个待接收的操作。因此,一旦你在通道上发送一个值,它就会被接收,这样就释放了通道,可以进行下一次发送。

英文:

The lack of buffering is inconsequential. There is a pending receive on the unbuffered channel. Therefore, as soon as you send a value on the channel it is received, which frees the channel for the next send.

huangapple
  • 本文由 发表于 2013年12月1日 22:09:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/20313007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定