xyctruth / stream Goto Github PK
View Code? Open in Web Editor NEWA Stream processing library based on Go 1.18+ Generics (parallel, pipeline, lazy) :cherries:
Home Page: https://pkg.go.dev/github.com/xyctruth/stream
License: MIT License
A Stream processing library based on Go 1.18+ Generics (parallel, pipeline, lazy) :cherries:
Home Page: https://pkg.go.dev/github.com/xyctruth/stream
License: MIT License
Line 43 in 9c2094b
"testing"
)
func BenchmarkShortCircuiting(b *testing.B) {
tests := []struct {
name string
count int
}{
{count: 100},
{count: 200},
{count: 300},
{count: 400},
{count: 500},
{count: 1000},
{count: 2000},
}
for _, tt := range tests {
b.Run(fmt.Sprintf("%s(%d)", "count:", tt.count), func(b *testing.B) {
s := newArray(tt.count)
s[0] = 101
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = NewSlice(s).
Filter(func(v int) bool { return true }).
Map(func(v int) int {
return v
}).
AllMatch(func(v int) bool { return v < 100 })
}
})
}
}
var funcVal = mapperFunc
func mapperFunc(v int) int { return v * 2 }
// TODO optimize func variable 3.287 ns/op, other(normal,anonymous) 1.553 ns/op
func BenchmarkPipelineFuncVariable(b *testing.B) {
tests := []struct {
name string
goroutines int
stage func(index int, e int) (isReturn bool, isComplete bool, ret int)
}{
{
name: "variable func",
stage: func(index int, e int) (isReturn bool, isComplete bool, ret int) {
return true, false, funcVal(e)
},
},
{
name: "normal func",
stage: func(index int, e int) (isReturn bool, isComplete bool, ret int) {
return true, false, mapperFunc(e)
},
},
{
name: "anonymous func",
stage: func(index int, e int) (isReturn bool, isComplete bool, ret int) {
return true, false, func(v int) int { return v * 2 }(e)
},
},
}
for _, tt := range tests {
b.Run(fmt.Sprintf("%s(%d)", tt.name, tt.goroutines), func(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
tt.stage(0, 0)
}
})
}
}
Line 43 in ca1339d
}
}
var funcVariable = mapperFunc
func mapperFunc(v int) int { return v * 2 }
// TODO optimize func variable 3.287 ns/op, other 1.553 ns/op
func BenchmarkPipelineFuncVariable(b *testing.B) {
tests := []struct {
name string
现在go泛型不支持struct的method添加类型参数,是否可以在NewSlice传入MapElem?
type sliceStream[Elem any, MapElem any] struct
func NewSlice[Elem any, MapElem any](v []Elem) sliceStream[Elem, MapElem]
func (stream sliceStream[Elem, MapElem]) Map(mapper func(Elem) MapElem) sliceStream[MapElem, MapElem]
func (stream SliceStream[E]) Insert(index int, elements ...E) SliceStream[E] {
// 如果不在一开始stream.evaluation(),stream.source 个数是未知。有可能会出现bug。
if len(stream.source) <= index {
return stream.Append(elements...)
}
stream.evaluation()
stream.source = slices.Insert(stream.source, index, elements...)
return stream
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.