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左右,几乎可以忽略不记。
有疑问加站长微信联系(非本文作者)