Golang

关注公众号 jb51net

关闭
首页 > 脚本专栏 > Golang > go内存队列list slice

go内存队列list VS slice实现方式对比分析

作者:a朋

这篇文章主要为大家介绍了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

结论:

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的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文