benchmarking - Benchmark Go code and goroutines -
i benchmark function: test()
, different numbers of threads working on it.
without goroutines:
var t1 = time.now() test() var elapsed1 = time.since(t1)
1 ns / operation
with goroutines:
runtime.gomaxprocs(1) var t1 = time.now() go test() var elapsed1 = time.since(t1)
1.10^-6 ns / operation
my test function:
func test() { := 0; < 1000000000; i++ { float_result = f1 + f2 float_result = f1 - f2 float_result = f1 * f2 float_result = f1 / f2 float_result = f1 + f2 float_result = f1 - f2 float_result = f1 * f2 float_result = f1 / f2 float_result = f1 + f2 float_result = f1 - f2 float_result = f1 * f2 float_result = f1 / f2 float_result = f1 + f2 float_result = f1 - f2 float_result = f1 * f2 float_result = f1 / f2 float_result = f1 + f2 float_result = f1 - f2 float_result = f1 * f2 float_result = f1 / f2 } }
- is test() function benchmarked when use goroutines in case?
- how possible reach 0.001ns / operation? looks way fast. (2.5ghz intel core i7)
- is use of goroutines
runtime.gomaxprocs(n)
equivalent use n threads ?
you not measuring time test()
runs, time takes call/create new goroutine go test()
.
you need wait goroutine(s) finish, example using sync.waitgroup.
// somewhere in main package var wg sync.waitgroup func test() { // first lines in test() should wg.add(1) defer wg.done() ... } // benchmark runtime.gomaxprocs(1) var t1 = time.now() go test() wg.wait() var elapsed1 = time.since(t1)
Comments
Post a Comment