summaryrefslogtreecommitdiff
path: root/Benchmarking code in Golang.md
diff options
context:
space:
mode:
authorJasper Ras <jaspert.ras@gmail.com>2025-04-18 21:01:49 +0200
committerJasper Ras <jaspert.ras@gmail.com>2025-04-18 21:01:49 +0200
commita8a8e1f984f20c8008f3a5f57cd39b416eb73104 (patch)
tree2262794b52d82c5fd9cbc55b503b52fafbd3af8a /Benchmarking code in Golang.md
parent5fa69499917fab7a026c90c8321dbcc22734106a (diff)
parentab409a3701bf59dd73dc1e0324376bdac8b6d74f (diff)
vault backup: 2025-04-18 21:01:49
Diffstat (limited to 'Benchmarking code in Golang.md')
-rw-r--r--Benchmarking code in Golang.md40
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