feat: Improve the program execution time by using gorutines

This commit is contained in:
rjianu
2023-07-12 16:56:36 +03:00
parent 209c1b93c6
commit 9f35c4eeeb
3 changed files with 45 additions and 14 deletions

6
benchmem03.txt Normal file
View File

@@ -0,0 +1,6 @@
goos: linux
goarch: amd64
pkg: github.com/Serares/coolStats
BenchmarkRun-16 10 92313063 ns/op 215030855 B/op 2529386 allocs/op
PASS
ok github.com/Serares/coolStats 1.187s

37
main.go
View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"sync"
) )
func main() { func main() {
@@ -37,25 +38,49 @@ func run(filenames []string, op string, column int, out io.Writer) error {
} }
consolidate := make([]float64, 0) consolidate := make([]float64, 0)
resCh := make(chan []float64)
errCh := make(chan error)
doneCh := make(chan struct{})
wg := sync.WaitGroup{}
for _, fname := range filenames { for _, fname := range filenames {
wg.Add(1)
go func(fname string) {
defer wg.Done()
f, err := os.Open(fname) f, err := os.Open(fname)
if err != nil { if err != nil {
return fmt.Errorf("cannot open file: %w", err) errCh <- fmt.Errorf("cannot open file: %w", err)
return
} }
data, err := csv2float(f, column) data, err := csv2float(f, column)
if err != nil { if err != nil {
return err errCh <- err
} }
if err := f.Close(); err != nil { if err := f.Close(); err != nil {
errCh <- err
}
resCh <- data
}(fname)
}
go func() {
wg.Wait()
close(doneCh)
}()
for {
select {
case err := <-errCh:
return err return err
} case data := <-resCh:
consolidate = append(consolidate, data...) consolidate = append(consolidate, data...)
} case <-doneCh:
_, err := fmt.Fprintln(out, opFunc(consolidate)) _, err := fmt.Fprintln(out, opFunc(consolidate))
return err return err
} }
}
}

BIN
trace02.out Normal file

Binary file not shown.