Commit b349cd2b authored by Rob Pike's avatar Rob Pike

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
parent 2cf66c1d
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package fmt_test package fmt_test
import ( import (
"bytes"
. "fmt" . "fmt"
"io" "io"
"math" "math"
...@@ -503,12 +504,39 @@ func TestCountMallocs(t *testing.T) { ...@@ -503,12 +504,39 @@ func TestCountMallocs(t *testing.T) {
Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100) Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100)
runtime.UpdateMemStats() runtime.UpdateMemStats()
mallocs = 0 - runtime.MemStats.Mallocs 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++ { for i := 0; i < 100; i++ {
Sprintf("%x %x", i, i) Sprintf("%x %x", i, i)
} }
runtime.UpdateMemStats() runtime.UpdateMemStats()
mallocs += runtime.MemStats.Mallocs mallocs += runtime.MemStats.Mallocs
Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/100) 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{} type flagPrinter struct{}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment