feat: Improve performance for min max using goroutines
This commit is contained in:
7
benchresultsMinMax_Improvement.txt
Normal file
7
benchresultsMinMax_Improvement.txt
Normal 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
BIN
cpu01max.pprof
Normal file
Binary file not shown.
28
main.go
28
main.go
@@ -95,7 +95,33 @@ func run(filenames []string, op string, column int, out io.Writer) error {
|
|||||||
return err
|
return err
|
||||||
case data := <-resCh:
|
case data := <-resCh:
|
||||||
if op == "min" || op == "max" {
|
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 {
|
} else {
|
||||||
consolidate = append(consolidate, data...)
|
consolidate = append(consolidate, data...)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user