Created
September 14, 2011 21:59
-
-
Save kylelemons/1217919 to your computer and use it in GitHub Desktop.
Benchmarking prints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(".") } })) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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