feat: Improve performance for min max using goroutines

This commit is contained in:
rjianu
2023-07-18 14:43:10 +03:00
parent 20d698d435
commit 7293844a1f
3 changed files with 34 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
goos: linux
goarch: amd64
pkg: github.com/Serares/coolStats
cpu: 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz
BenchmarkRun-16 10 134744628 ns/op
PASS
ok github.com/Serares/coolStats 1.487s

BIN
cpu01max.pprof Normal file

Binary file not shown.

28
main.go
View File

@@ -95,7 +95,33 @@ func run(filenames []string, op string, column int, out io.Writer) error {
return err
case data := <-resCh:
if op == "min" || op == "max" {
consolidate = append(consolidate, opFunc(data))
// todo see if using goroutines here can improve the performance
// spawn like 4 goroutines and divide the date between them to be processed
minWg := sync.WaitGroup{}
theData := make(chan float64)
quarterLength := len(data) / 4
start := 0
endQuarter := quarterLength
incrementQuarter := 2
for i := 0; i < 4; i++ {
minWg.Add(1)
go func(start, end int) {
defer minWg.Done()
theData <- opFunc(data[start:end])
}(start, endQuarter)
start = endQuarter
endQuarter = quarterLength * incrementQuarter
incrementQuarter++
}
go func() {
minWg.Wait()
close(theData)
}()
for quarterData := range theData {
consolidate = append(consolidate, quarterData)
}
} else {
consolidate = append(consolidate, data...)
}