Golang pipline泛型管道和类型管道的性能差距

栏目: Go · 发布时间: 5年前

内容简介:pips/generic.gopips/typed.gopipline4_test.go

pips/generic.go

//generic
package pips

type Generic struct {
}

func NewGeneric() *Generic {
    generic := &Generic{}
    return generic
}

func (generic *Generic) Repeat(
    done <-chan interface{},
    args ...interface{},
) <-chan interface{} {
    valueStream := make(chan interface{})
    go func() {
        defer close(valueStream)
        for {
            for _, v := range args {
                select {
                case <-done:
                    return
                case valueStream <- v:
                }
            }
        }
    }()
    return valueStream
}

func (generic *Generic) Take(
    done <-chan interface{},
    valueStream <-chan interface{},
    num int,
) <-chan interface{} {
    takeStream := make(chan interface{})

    go func() {
        defer close(takeStream)
        for i := 0; i < num; i++ {
            select {
            case <-done:
                return
            case takeStream <- <-valueStream:
            }
        }
    }()
    return takeStream
}

func (generic *Generic) ToString(
    done <-chan interface{},
    valueStream <-chan interface{},
) <-chan string {
    stringStream := make(chan string)
    go func() {
        defer close(stringStream)
        for i := range valueStream {
            select {
            case <-done:
                return
            case stringStream <- i.(string):
            }
        }
    }()
    return stringStream
}

pips/typed.go

// typed
package pips

type Typed struct {
}

func NewTyped() *Typed {
    typed := &Typed{}
    return typed
}

func (typed *Typed) Repeat(
    done <-chan interface{},
    args ...string,
) <-chan string {
    valueStream := make(chan string)
    go func() {
        defer close(valueStream)
        for {
            for _, v := range args {
                select {
                case <-done:
                    return
                case valueStream <- v:
                }
            }
        }

    }()
    return valueStream
}

func (typed *Typed) Take(
    done <-chan interface{},
    valueStream <-chan string,
    num int,
) <-chan string {
    takeStream := make(chan string)
    go func() {
        defer close(takeStream)
        for i := 0; i < num; i++ {
            select {
            case <-done:
                return
            case takeStream <- <-valueStream:
            }
        }
    }()
    return takeStream
}

pipline4_test.go

package main

import (
    pips "pipline4/pips"
    "testing"
)

func BenchmarkGeneric(b *testing.B) {
    generic := pips.NewGeneric()
    done := make(chan interface{})
    defer close(done)

    b.ResetTimer()
    for range generic.ToString(done, generic.Take(done, generic.Repeat(done, "a"), b.N)) {
    }
}

func BenchmarkTyped(b *testing.B) {
    typed := pips.NewTyped()
    done := make(chan interface{})
    defer close(done)

    b.ResetTimer()
    for range typed.Take(done, typed.Repeat(done, "a"), b.N) {
    }
}

运行结果如下图所示,表明泛型管道和类型管道的性能差距大概在 50%。也就是700ms左右,几乎可以忽略不记。

Golang pipline泛型管道和类型管道的性能差距

image.png


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Django

Pro Django

Marty Alchin / Apress / 2008-11-24 / USD 49.99

Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pr......一起来看看 《Pro Django》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试