Skip to content

Instantly share code, notes, and snippets.

@ginokent
Last active November 27, 2021 18:58
Show Gist options
  • Save ginokent/762cefddc1ebcd5ae8f5748209f0f2f1 to your computer and use it in GitHub Desktop.
Save ginokent/762cefddc1ebcd5ae8f5748209f0f2f1 to your computer and use it in GitHub Desktop.
Go benchmark ベンチマーク 覚書
# 前準備
mkdir -p /tmp/benchmark
cd /tmp/benchmark
# ベンチマークファイル作成
tee /tmp/benchmark/benchmark_test.go <<"EOF"
package benchmark_test
import (
"strconv"
"testing"
)
func BenchmarkAppendQuote(b *testing.B) {
s := []byte{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s = strconv.AppendQuote(s, `\"a\"`)
}
}
EOF
# ベンチマーク走らせる
go test -bench . -benchmem -test.run=none -test.benchtime=1000ms
# alloc と free のトレースを見る
go test -c
GODEBUG=allocfreetrace=1 ./"$(basename "${PWD}")".test -test.run=none -test.bench=BenchmarkAppendQuote -test.benchtime=1ms > trace.log 2>&1
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment