aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2011-09-02 11:47:15 +1000
committerRob Pike <r@golang.org>2011-09-02 11:47:15 +1000
commitb349cd2b0ad12c2ebe67ca2c6c1aa01bc9991520 (patch)
treeb77e0c3652cc5e67bb3bf8c550402393753b1b47
parent2cf66c1d946e1c5646f063f07bc0bd89406a9c9a (diff)
downloadgo-b349cd2b0ad12c2ebe67ca2c6c1aa01bc9991520.tar.gz
go-b349cd2b0ad12c2ebe67ca2c6c1aa01bc9991520.zip
fmt/fmt_test.go: count mallocs in a few more cases.
Interesting that Fprintf can do zero mallocs. (Sprintf must allocate the returned string.) R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/4977049
-rw-r--r--src/pkg/fmt/fmt_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go
index 1142c9f8ad..b1ad345186 100644
--- a/src/pkg/fmt/fmt_test.go
+++ b/src/pkg/fmt/fmt_test.go
@@ -5,6 +5,7 @@
package fmt_test
import (
+ "bytes"
. "fmt"
"io"
"math"
@@ -504,11 +505,38 @@ func TestCountMallocs(t *testing.T) {
runtime.UpdateMemStats()
mallocs = 0 - runtime.MemStats.Mallocs
for i := 0; i < 100; i++ {
+ Sprintf("%s", "hello")
+ }
+ runtime.UpdateMemStats()
+ mallocs += runtime.MemStats.Mallocs
+ Printf("mallocs per Sprintf(\"%%s\"): %d\n", mallocs/100)
+ runtime.UpdateMemStats()
+ mallocs = 0 - runtime.MemStats.Mallocs
+ for i := 0; i < 100; i++ {
Sprintf("%x %x", i, i)
}
runtime.UpdateMemStats()
mallocs += runtime.MemStats.Mallocs
Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/100)
+ buf := new(bytes.Buffer)
+ runtime.UpdateMemStats()
+ mallocs = 0 - runtime.MemStats.Mallocs
+ for i := 0; i < 100; i++ {
+ buf.Reset()
+ Fprintf(buf, "%x %x %x", i, i, i)
+ }
+ runtime.UpdateMemStats()
+ mallocs += runtime.MemStats.Mallocs
+ Printf("mallocs per Fprintf(buf, \"%%x %%x %%x\"): %d\n", mallocs/100)
+ runtime.UpdateMemStats()
+ mallocs = 0 - runtime.MemStats.Mallocs
+ for i := 0; i < 100; i++ {
+ buf.Reset()
+ Fprintf(buf, "%s", "hello")
+ }
+ runtime.UpdateMemStats()
+ mallocs += runtime.MemStats.Mallocs
+ Printf("mallocs per Fprintf(buf, \"%%s\"): %d\n", mallocs/100)
}
type flagPrinter struct{}