https://github.com/gogf/gf/v2/blob/master/container/gqueue/gqueue_z_bench_test.go
Benchmark testing of gqueue
and the standard library channel
, where each benchmark test has a b.N
value of 20000000
to ensure consistent dynamic queue access and prevent deadlock
:
goos: linux
goarch: amd64
pkg: github.com/gogf/gf/v2/container/gqueue
Benchmark_Gqueue_StaticPushAndPop-4 20000000 84.2 ns/op
Benchmark_Gqueue_DynamicPush-4 20000000 164 ns/op
Benchmark_Gqueue_DynamicPop-4 20000000 121 ns/op
Benchmark_Channel_PushAndPop-4 20000000 70.0 ns/op
PASS
It can be seen that the read and write performance of the standard library channel
is very high, but due to the need to initialize memory during creation, the efficiency is very, very low when creating a channel
(initialization involves memory allocation), and it is limited by the queue size, meaning that the written data cannot exceed the specified queue size. gqueue
is more flexible to use compared to channel
, offering higher creation efficiency (dynamically allocated memory) and not being restricted by queue size (though size can be limited).