Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created September 14, 2011 21:59
Show Gist options
  • Save kylelemons/1217919 to your computer and use it in GitHub Desktop.
Save kylelemons/1217919 to your computer and use it in GitHub Desktop.
Benchmarking prints
package main
import (
"fmt"
"io"
"os"
. "testing"
)
// Run this with >/dev/null
func main() {
fmt.Fprintln(os.Stderr, "fmt.Printf ", Benchmark(func(b *B){ for i := 0; i < b.N; i++ { fmt.Printf(".") } }))
fmt.Fprintln(os.Stderr, "fmt.Print ", Benchmark(func(b *B){ for i := 0; i < b.N; i++ { fmt.Print(".") } }))
fmt.Fprintln(os.Stderr, "io.WriteString", Benchmark(func(b *B){ for i := 0; i < b.N; i++ { io.WriteString(os.Stdout, ".") } }))
fmt.Fprintln(os.Stderr, "os.WriteString", Benchmark(func(b *B){ for i := 0; i < b.N; i++ { os.Stdout.WriteString(".") } }))
}
$ 6g *.go && 6l *.6 && ./6.out >/dev/null
fmt.Printf 5000000 594 ns/op
fmt.Print 2000000 836 ns/op
io.WriteString 5000000 501 ns/op
os.WriteString 5000000 442 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment