---
title: "队列类型-性能测试"
url: "https://goframe.org/docs/components/container-gqueue-benchmark"
markdown_url: "https://goframe.org/docs/components/container-gqueue-benchmark.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "组件列表"
category: "组件列表 / 数据结构 / 队列类型-gqueue"
published: "2024-10-21"
updated: "2025-02-27"
description: "在GoFrame框架中gqueue与标准库channel的性能测试。通过基准测试展示了gqueue在动态存储和弹性容量上的优势，相对于channel的固定内存分配和容量限制，gqueue在创建效率和灵活性表现更佳。"
keywords: ["GoFrame", "GoFrame框架", "gqueue", "性能测试", "队列类型", "channel", "基准测试", "动态队列", "队列性能", "benchmark"]
---

# 队列类型-性能测试

[https://github.com/gogf/gf/blob/master/container/gqueue/gqueue\_z\_bench\_test.go](https://github.com/gogf/gf/blob/master/container/gqueue/gqueue_z_bench_test.go)

`gqueue` 与标准库 `channel` 的性能基准测试，其中每一次基准测试的 `b.N` 值均为 `20000000`，以保证动态队列存取一致防止 `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
```

可以看到标准库的 `channel` 的读写性能是非常高的，但是创建的时候由于需要初始化内存，因此创建 `channel` 的时候效率非常非常低（初始化即分配内存），并且受到队列大小的限制，写入的数据不能超过指定的队列大小。 `gqueue` 使用起来比 `channel` 更加灵活，不仅创建效率高（动态分配内存），不受队列大小限制(也可限定大小)。
