From 8475832f0d8957840d0aec1b027a89e5c03ed3bf Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 27 Jun 2011 18:50:27 -0400 Subject: [PATCH] testing: scale benchmark precision to 0.01ns if needed R=dvyukov, r CC=golang-dev https://golang.org/cl/4657053 --- src/pkg/testing/benchmark.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pkg/testing/benchmark.go b/src/pkg/testing/benchmark.go index 0ee879709d8..4d5ff6c7768 100644 --- a/src/pkg/testing/benchmark.go +++ b/src/pkg/testing/benchmark.go @@ -174,7 +174,18 @@ func (r BenchmarkResult) String() string { if mbs != 0 { mb = fmt.Sprintf("\t%7.2f MB/s", mbs) } - return fmt.Sprintf("%8d\t%10d ns/op%s", r.N, r.NsPerOp(), mb) + nsop := r.NsPerOp() + ns := fmt.Sprintf("%10d ns/op", nsop) + if r.N > 0 && nsop < 100 { + // The format specifiers here make sure that + // the ones digits line up for all three possible formats. + if nsop < 10 { + ns = fmt.Sprintf("%13.2f ns/op", float64(r.Ns)/float64(r.N)) + } else { + ns = fmt.Sprintf("%12.1f ns/op", float64(r.Ns)/float64(r.N)) + } + } + return fmt.Sprintf("%8d\t%s%s", r.N, ns, mb) } // An internal function but exported because it is cross-package; part of the implementation