内容简介: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左右,几乎可以忽略不记。
image.png
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 前端圈的贫富差距正在拉大?
- 机器与人类视觉能力的差距(三)
- Python 成功上位,正逐渐与 Java 拉开差距
- Python 成功上位,正逐渐与 Java 拉开差距
- Python 成功上位,正逐渐与 Java 拉开差距
- AI和工业4.0之间,还有多远的差距?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。