]> Cypherpunks repositories - gostls13.git/commitdiff
fmt/fmt_test.go: count mallocs in a few more cases.
authorRob Pike <r@golang.org>
Fri, 2 Sep 2011 01:47:15 +0000 (11:47 +1000)
committerRob Pike <r@golang.org>
Fri, 2 Sep 2011 01:47:15 +0000 (11:47 +1000)
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

src/pkg/fmt/fmt_test.go

index 1142c9f8ad72ad38754c8e925a4bb375e8cc3169..b1ad345186f61a0c2b38f6887a88be788f5f79e1 100644 (file)
@@ -5,6 +5,7 @@
 package fmt_test
 
 import (
+       "bytes"
        . "fmt"
        "io"
        "math"
@@ -503,12 +504,39 @@ func TestCountMallocs(t *testing.T) {
        Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100)
        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{}