diff options
author | Jasper Ras <jras@hostnet.nl> | 2025-04-18 20:58:54 +0200 |
---|---|---|
committer | Jasper Ras <jras@hostnet.nl> | 2025-04-18 20:58:54 +0200 |
commit | ab409a3701bf59dd73dc1e0324376bdac8b6d74f (patch) | |
tree | 17fba0a6811534858300a550c06376b25f9644c6 /Benchmarking code in Golang.md | |
parent | c8c774ad7c0d98def2e83647d4f635ee275b66de (diff) |
vault backup: 2025-04-18 20:58:54
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 |