Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Created February 17, 2018 23:45
Show Gist options
  • Save odeke-em/d6ac888b7a36dddc80dc16ab221e67db to your computer and use it in GitHub Desktop.
Save odeke-em/d6ac888b7a36dddc80dc16ab221e67db to your computer and use it in GitHub Desktop.
Showing the difference between writeStr and io.Writer.Write
BenchmarkWrite1B-4 100000000 11.4 ns/op 2 B/op 0 allocs/op
BenchmarkWrite1B-4 100000000 10.5 ns/op 2 B/op 0 allocs/op
BenchmarkWrite1B-4 100000000 10.7 ns/op 2 B/op 0 allocs/op
BenchmarkWrite1B-4 100000000 11.8 ns/op 2 B/op 0 allocs/op
BenchmarkWrite1B-4 100000000 11.5 ns/op 2 B/op 0 allocs/op
BenchmarkWrite10B-4 100000000 27.5 ns/op 24 B/op 0 allocs/op
BenchmarkWrite10B-4 100000000 14.0 ns/op 24 B/op 0 allocs/op
BenchmarkWrite10B-4 100000000 13.5 ns/op 24 B/op 0 allocs/op
BenchmarkWrite10B-4 100000000 13.8 ns/op 24 B/op 0 allocs/op
BenchmarkWrite10B-4 100000000 13.5 ns/op 24 B/op 0 allocs/op
BenchmarkWrite1KB-4 5000000 5008 ns/op 3435 B/op 0 allocs/op
BenchmarkWrite1KB-4 500000 2172 ns/op 2147 B/op 0 allocs/op
BenchmarkWrite1KB-4 1000000 1328 ns/op 2147 B/op 0 allocs/op
BenchmarkWrite1KB-4 3000000 4793 ns/op 2863 B/op 0 allocs/op
BenchmarkWrite1KB-4 300000 3706 ns/op 3579 B/op 0 allocs/op
BenchmarkWrite10KB-4 200000 31738 ns/op 26842 B/op 0 allocs/op
BenchmarkWrite10KB-4 50000 23848 ns/op 26840 B/op 0 allocs/op
BenchmarkWrite10KB-4 200000 30181 ns/op 26842 B/op 0 allocs/op
BenchmarkWrite10KB-4 100000 20815 ns/op 26841 B/op 0 allocs/op
BenchmarkWrite10KB-4 200000 18399 ns/op 26842 B/op 0 allocs/op
BenchmarkWrite1B-4 30000000 42.3 ns/op 10 B/op 1 allocs/op
BenchmarkWrite1B-4 50000000 40.3 ns/op 10 B/op 1 allocs/op
BenchmarkWrite1B-4 50000000 40.2 ns/op 10 B/op 1 allocs/op
BenchmarkWrite1B-4 30000000 40.2 ns/op 10 B/op 1 allocs/op
BenchmarkWrite1B-4 30000000 39.6 ns/op 10 B/op 1 allocs/op
BenchmarkWrite10B-4 30000000 59.2 ns/op 36 B/op 1 allocs/op
BenchmarkWrite10B-4 30000000 51.5 ns/op 36 B/op 1 allocs/op
BenchmarkWrite10B-4 30000000 51.6 ns/op 36 B/op 1 allocs/op
BenchmarkWrite10B-4 30000000 51.8 ns/op 36 B/op 1 allocs/op
BenchmarkWrite10B-4 30000000 51.8 ns/op 36 B/op 1 allocs/op
BenchmarkWrite1KB-4 1000000 1404 ns/op 3171 B/op 1 allocs/op
BenchmarkWrite1KB-4 2000000 1173 ns/op 3171 B/op 1 allocs/op
BenchmarkWrite1KB-4 2000000 1164 ns/op 3171 B/op 1 allocs/op
BenchmarkWrite1KB-4 1000000 1064 ns/op 3171 B/op 1 allocs/op
BenchmarkWrite1KB-4 2000000 680 ns/op 3171 B/op 1 allocs/op
BenchmarkWrite10KB-4 200000 14869 ns/op 37082 B/op 1 allocs/op
BenchmarkWrite10KB-4 200000 21493 ns/op 37082 B/op 1 allocs/op
BenchmarkWrite10KB-4 50000 24814 ns/op 37080 B/op 1 allocs/op
BenchmarkWrite10KB-4 200000 20319 ns/op 37082 B/op 1 allocs/op
BenchmarkWrite10KB-4 200000 23330 ns/op 37082 B/op 1 allocs/op
$ benchstat old.txt new.txt 
name         old time/op    new time/op    delta
Write1B-4      40.5ns ± 4%    11.2ns ± 6%   -72.41%  (p=0.008 n=5+5)
Write10B-4     51.7ns ± 0%    13.7ns ± 2%   -73.49%  (p=0.029 n=4+4)
Write1KB-4     1.10µs ±38%    3.40µs ±61%  +210.06%  (p=0.016 n=5+5)
Write10KB-4    21.0µs ±29%    25.0µs ±27%      ~     (p=0.421 n=5+5)

name         old alloc/op   new alloc/op   delta
Write1B-4       10.0B ± 0%      2.0B ± 0%   -80.00%  (p=0.008 n=5+5)
Write10B-4      36.0B ± 0%     24.0B ± 0%   -33.33%  (p=0.008 n=5+5)
Write1KB-4     3.17kB ± 0%    2.83kB ±26%      ~     (p=0.563 n=5+5)
Write10KB-4    37.1kB ± 0%    26.8kB ± 0%   -27.62%  (p=0.000 n=4+5)

name         old allocs/op  new allocs/op  delta
Write1B-4        1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.008 n=5+5)
Write10B-4       1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.008 n=5+5)
Write1KB-4       1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.008 n=5+5)
Write10KB-4      1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.008 n=5+5)
package main
import (
"bytes"
"io"
"strings"
"testing"
)
func writeStr(w io.Writer, s string) (err error) {
_, err = w.Write([]byte(s))
return
}
func BenchmarkWriteStr1B(b *testing.B) {
benchmarkWriteStr(b, 1)
}
func BenchmarkWriteStr10B(b *testing.B) {
benchmarkWriteStr(b, 10)
}
func BenchmarkWriteStr1KB(b *testing.B) {
benchmarkWriteStr(b, 1024)
}
func BenchmarkWriteStr10KB(b *testing.B) {
benchmarkWriteStr(b, 10*1024)
}
func benchmarkWriteStr(b *testing.B, n int) {
str := strings.Repeat("a", n)
buf := new(bytes.Buffer)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := writeStr(buf, str); err != nil {
b.Fatalf("#%d err: %v", i, err)
}
}
b.ReportAllocs()
}
func BenchmarkWriterDotWrite1B(b *testing.B) {
benchmarkWriterDotWrite(b, 1)
}
func BenchmarkWriterDotWrite10B(b *testing.B) {
benchmarkWriterDotWrite(b, 10)
}
func BenchmarkWriterDotWrite1KB(b *testing.B) {
benchmarkWriterDotWrite(b, 1024)
}
func BenchmarkWriterDotWrite10KB(b *testing.B) {
benchmarkWriterDotWrite(b, 10*1024)
}
func benchmarkWriterDotWrite(b *testing.B, n int) {
bs := bytes.Repeat([]byte("a"), n)
buf := new(bytes.Buffer)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := buf.Write(bs); err != nil {
b.Fatalf("#%d err: %v", i, err)
}
}
b.ReportAllocs()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment