feat: Improve the program execution time by using gorutines
This commit is contained in:
6
benchmem03.txt
Normal file
6
benchmem03.txt
Normal 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
37
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -37,25 +38,49 @@ func run(filenames []string, op string, column int, out io.Writer) error {
|
||||
}
|
||||
|
||||
consolidate := make([]float64, 0)
|
||||
resCh := make(chan []float64)
|
||||
errCh := make(chan error)
|
||||
doneCh := make(chan struct{})
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
for _, fname := range filenames {
|
||||
wg.Add(1)
|
||||
go func(fname string) {
|
||||
defer wg.Done()
|
||||
f, err := os.Open(fname)
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
errCh <- err
|
||||
}
|
||||
|
||||
if err := f.Close(); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
|
||||
resCh <- data
|
||||
}(fname)
|
||||
|
||||
}
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(doneCh)
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case err := <-errCh:
|
||||
return err
|
||||
}
|
||||
|
||||
case data := <-resCh:
|
||||
consolidate = append(consolidate, data...)
|
||||
}
|
||||
|
||||
case <-doneCh:
|
||||
_, err := fmt.Fprintln(out, opFunc(consolidate))
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BIN
trace02.out
Normal file
BIN
trace02.out
Normal file
Binary file not shown.
Reference in New Issue
Block a user