go内存队列list VS slice实现方式对比分析
go队列的实现方式
golang中没有队列这种数据结构,通常需要自己实现,常见的可以通过list或slice实现。
list是"container/list"中的数据结构,用双向链表实现,可以用来做队列:
//入队 func (l *List) PushBack(v interface{}) *Element //出队:先Front()取得头,然后Remove()删除 func (l *List) Front() *Element func (l *List) Remove(e *Element) interface{}
slice实现队列的方式:
var s []obj s = append(s, obj) //入队 s = s[1:] //出队
benchmark测试比较
benchmark测试代码: 队列中存入object对象
type EventMsg struct { Id string Msg string } func BenchmarkQueue_ListObject(b *testing.B) { var l = list.New() for i := 0; i < b.N; i++ { l.PushBack(EventMsg{ Id: strconv.Itoa(i), Msg: "1:abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz", }) l.PushBack(EventMsg{ Id: strconv.Itoa(i), Msg: "1:opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn", }) l.Remove(l.Front()) } } func BenchmarkQueue_SliceObject(b *testing.B) { var q []EventMsg for i := 0; i < b.N; i++ { q = append(q, EventMsg{ Id: strconv.Itoa(i), Msg: "1:abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz", }) q = append(q, EventMsg{ Id: strconv.Itoa(i), Msg: "1:opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn", }) q = q[1:] } }
benchmark测试代码:队列中存入Object指针对象
func BenchmarkQueue_ListObjPtr(b *testing.B) { var l = list.New() for i := 0; i < b.N; i++ { l.PushBack(&EventMsg{ Id: strconv.Itoa(i), Msg: "1:abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz", }) l.PushBack(&EventMsg{ Id: strconv.Itoa(i), Msg: "1:opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn", }) l.Remove(l.Front()) } } func BenchmarkQueue_SliceObjPtr(b *testing.B) { var q []*EventMsg for i := 0; i < b.N; i++ { q = append(q, &EventMsg{ Id: strconv.Itoa(i), Msg: "1:abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz", }) q = append(q, &EventMsg{ Id: strconv.Itoa(i), Msg: "1:opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn-opqrstuvwxyzabcdefghijklmn", }) q = q[1:] } }
benchmark测试结果
# go test -bench=BenchmarkQueue -count=1 -benchmem -cpu 4
BenchmarkQueue_ListObject-4 1000000 1423 ns/op 175 B/op 5 allocs/op
BenchmarkQueue_ListObjPtr-4 1000000 1124 ns/op 175 B/op 5 allocs/op
BenchmarkQueue_SliceObject-4 1000000 1574 ns/op 357 B/op 1 allocs/op
BenchmarkQueue_SliceObjPtr-4 1831449 662.7 ns/op 161 B/op 3 allocs/op
PASS
ok github.com/go_list/bench_test 6.144s
结论:
- 不管用list还是slice,队列中存储对象指针的性能,要好于直接存储对象;
- slice实现的队列,存储指针对象时性能最好;
- list实现的队列,不管是存储对象还是指针对象,其性能差异不是太大;
Open-falcon的队列实现
open-falcon使用list和mutex实现了一个协程安全的内存队列。
实现代码:https://github.com/toolkits/c...
type SafeList struct { sync.RWMutex L *list.List } func NewSafeList() *SafeList { return &SafeList{L: list.New()} } //入队 func (this *SafeList) PushFront(v interface{}) *list.Element { this.Lock() e := this.L.PushFront(v) this.Unlock() return e } //出队 func (this *SafeList) PopBack() interface{} { this.Lock() if elem := this.L.Back(); elem != nil { item := this.L.Remove(elem) this.Unlock() return item } this.Unlock() return nil }
参考:https://www.jb51.net/jiaoben/2957122cc.htm
以上就是go内存队列list VS slice实现方式对比分析的详细内容,更多关于go内存队列list slice的资料请关注脚本之家其它相关文章!
相关文章
go-zero接入skywalking实现链路追踪的详细教程
SkyWalking是一个开源的服务追踪系统,它专注于分布式系统的可观测性和服务性能分析,它的主要目标是帮助开发者理解和优化微服务架构中的服务间交互情况,本文介绍了go-zero接入skywalking链路追踪的详细教程,需要的朋友可以参考下2024-08-08Golang range slice 与range array 之间的区别
这篇文章主要介绍了Golang range slice 与range array 之间的区别,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-07-07
最新评论