diff options
Diffstat (limited to 'Benchmarking code in Golang.md')
-rw-r--r-- | Benchmarking code in Golang.md | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Benchmarking code in Golang.md b/Benchmarking code in Golang.md new file mode 100644 index 0000000..22d180d --- /dev/null +++ b/Benchmarking code in Golang.md @@ -0,0 +1,40 @@ +--- +tags: + - golang + - howto +--- +Simple timing can be done using the time package. +``` +import time +import fmt + +func main() { + start := time.Now() + elapsed = time.Since(start).Seconds() + fmt.Printf("%.2fs", elapsed) +} +``` + +Writing benchmarks. They look similar to tests: +``` +import testing + +func BenchmarkSome(b *testing.B) { + for i := 0; i < b.N; i++ { + Some() + } +} +``` +b.N is supplied by test driver and is dynamically changed depending on runtime. + +`go test -bench=<regex>` + +This just benchmarks a function being called many times, comparative benchmarks can be written using a regular function being called from Benchmarks, something like so : +``` +import testing + +func doBench(b *testing.B, size int) { ... vary inputs size } +func Bench1(b *testing.B) +func Bench10(b *testing.B) +func Bench100(b * testing.B) +```
\ No newline at end of file |