mirror of
https://github.com/golang/go
synced 2024-11-24 21:00:09 -07:00
fmt: benchmark floating point.
mallocs per Sprintf("%x"): 1 mallocs per Sprintf("%g"): 4 R=golang-dev, gri CC=golang-dev https://golang.org/cl/5449106
This commit is contained in:
parent
972b98c135
commit
66410bac3d
@ -500,69 +500,84 @@ func BenchmarkSprintfPrefixedInt(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkSprintfFloat(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Sprintf("%g", 5.23184)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCountMallocs(t *testing.T) {
|
func TestCountMallocs(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const N = 100
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs := 0 - runtime.MemStats.Mallocs
|
mallocs := 0 - runtime.MemStats.Mallocs
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < N; i++ {
|
||||||
Sprintf("")
|
Sprintf("")
|
||||||
}
|
}
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs += runtime.MemStats.Mallocs
|
mallocs += runtime.MemStats.Mallocs
|
||||||
Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/100)
|
Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/N)
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs = 0 - runtime.MemStats.Mallocs
|
mallocs = 0 - runtime.MemStats.Mallocs
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < N; i++ {
|
||||||
Sprintf("xxx")
|
Sprintf("xxx")
|
||||||
}
|
}
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs += runtime.MemStats.Mallocs
|
mallocs += runtime.MemStats.Mallocs
|
||||||
Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/100)
|
Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/N)
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs = 0 - runtime.MemStats.Mallocs
|
mallocs = 0 - runtime.MemStats.Mallocs
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < N; i++ {
|
||||||
Sprintf("%x", i)
|
Sprintf("%x", i)
|
||||||
}
|
}
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs += runtime.MemStats.Mallocs
|
mallocs += runtime.MemStats.Mallocs
|
||||||
Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100)
|
Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/N)
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs = 0 - runtime.MemStats.Mallocs
|
mallocs = 0 - runtime.MemStats.Mallocs
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < N; i++ {
|
||||||
Sprintf("%s", "hello")
|
Sprintf("%s", "hello")
|
||||||
}
|
}
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs += runtime.MemStats.Mallocs
|
mallocs += runtime.MemStats.Mallocs
|
||||||
Printf("mallocs per Sprintf(\"%%s\"): %d\n", mallocs/100)
|
Printf("mallocs per Sprintf(\"%%s\"): %d\n", mallocs/N)
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs = 0 - runtime.MemStats.Mallocs
|
mallocs = 0 - runtime.MemStats.Mallocs
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < N; 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/N)
|
||||||
|
runtime.UpdateMemStats()
|
||||||
|
mallocs = 0 - runtime.MemStats.Mallocs
|
||||||
|
for i := 0; i < N; i++ {
|
||||||
|
Sprintf("%g", 3.14159)
|
||||||
|
}
|
||||||
|
runtime.UpdateMemStats()
|
||||||
|
mallocs += runtime.MemStats.Mallocs
|
||||||
|
Printf("mallocs per Sprintf(\"%%g\"): %d\n", mallocs/N)
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs = 0 - runtime.MemStats.Mallocs
|
mallocs = 0 - runtime.MemStats.Mallocs
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < N; i++ {
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
Fprintf(buf, "%x %x %x", i, i, i)
|
Fprintf(buf, "%x %x %x", i, i, i)
|
||||||
}
|
}
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs += runtime.MemStats.Mallocs
|
mallocs += runtime.MemStats.Mallocs
|
||||||
Printf("mallocs per Fprintf(buf, \"%%x %%x %%x\"): %d\n", mallocs/100)
|
Printf("mallocs per Fprintf(buf, \"%%x %%x %%x\"): %d\n", mallocs/N)
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs = 0 - runtime.MemStats.Mallocs
|
mallocs = 0 - runtime.MemStats.Mallocs
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < N; i++ {
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
Fprintf(buf, "%s", "hello")
|
Fprintf(buf, "%s", "hello")
|
||||||
}
|
}
|
||||||
runtime.UpdateMemStats()
|
runtime.UpdateMemStats()
|
||||||
mallocs += runtime.MemStats.Mallocs
|
mallocs += runtime.MemStats.Mallocs
|
||||||
Printf("mallocs per Fprintf(buf, \"%%s\"): %d\n", mallocs/100)
|
Printf("mallocs per Fprintf(buf, \"%%s\"): %d\n", mallocs/N)
|
||||||
}
|
}
|
||||||
|
|
||||||
type flagPrinter struct{}
|
type flagPrinter struct{}
|
||||||
|
Loading…
Reference in New Issue
Block a user