Loading main.go +14 −11 Original line number Diff line number Diff line Loading @@ -6,7 +6,6 @@ import ( "log" "os" "path" "reflect" "regexp" "runtime" "sync" Loading @@ -26,11 +25,6 @@ const ( destdir = "data/sample/offline/analyse" // where to write ) // getFunctionName() returns the name of a function. func getFunctionName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() } // Scan the `sourcedir` for `*.sota` files, and extract the JSON segements // under `destdir`. func example01() { Loading @@ -44,19 +38,28 @@ func example01() { } // work on the file list tasks := []func(re *regexp.Regexp, files []os.FileInfo){processFiles001, processFiles002, processFiles003} //tasks := []func(re *regexp.Regexp, files []os.FileInfo){processFiles003} tasks := []struct { name string test func(re *regexp.Regexp, files []os.FileInfo) }{ {"Test process files 001", processFiles001}, {"Test process files 002", processFiles002}, {"Test process files 003", processFiles003}, } // do the benchmark results := make(chan string, len(tasks)) for i, f := range tasks { fmt.Printf("%d. %s\n", i, getFunctionName(f)) fmt.Printf("%d. %s\n", i, f.name) start := time.Now() f(re, files) f.test(re, files) stop := time.Now() elapsed := stop.Sub(start) results <- fmt.Sprintf("f %d took %s", i, elapsed) results <- fmt.Sprintf("%s took %s", f.name, elapsed) } close(results) // display results fmt.Println("Results:") for result := range results { fmt.Println(result) Loading Loading
main.go +14 −11 Original line number Diff line number Diff line Loading @@ -6,7 +6,6 @@ import ( "log" "os" "path" "reflect" "regexp" "runtime" "sync" Loading @@ -26,11 +25,6 @@ const ( destdir = "data/sample/offline/analyse" // where to write ) // getFunctionName() returns the name of a function. func getFunctionName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() } // Scan the `sourcedir` for `*.sota` files, and extract the JSON segements // under `destdir`. func example01() { Loading @@ -44,19 +38,28 @@ func example01() { } // work on the file list tasks := []func(re *regexp.Regexp, files []os.FileInfo){processFiles001, processFiles002, processFiles003} //tasks := []func(re *regexp.Regexp, files []os.FileInfo){processFiles003} tasks := []struct { name string test func(re *regexp.Regexp, files []os.FileInfo) }{ {"Test process files 001", processFiles001}, {"Test process files 002", processFiles002}, {"Test process files 003", processFiles003}, } // do the benchmark results := make(chan string, len(tasks)) for i, f := range tasks { fmt.Printf("%d. %s\n", i, getFunctionName(f)) fmt.Printf("%d. %s\n", i, f.name) start := time.Now() f(re, files) f.test(re, files) stop := time.Now() elapsed := stop.Sub(start) results <- fmt.Sprintf("f %d took %s", i, elapsed) results <- fmt.Sprintf("%s took %s", f.name, elapsed) } close(results) // display results fmt.Println("Results:") for result := range results { fmt.Println(result) Loading