All Benchmarks

Go Benchmark

Printing

Compare fmt's Print, Printf, and Println variants, with and without a target writer.

fmtprintio

The Go fmt package provides several functions for printing output. This benchmark compares Print, Println, and Printf (which write to stdout) against their Fprint, Fprintln, and Fprintf counterparts (which write to an io.Writer, here io.Discard). The Fprint variants avoid the overhead of going through os.Stdout, making them noticeably faster.

linux/amd64AMD Ryzen 9 9950X3D 16-Core Processorbenchmarks/printing
Compare atCPUs

1 CPU

Fastest

Fprintln

16.83 ns/op

Slowest

Print

108.4 ns/op · 6.4x slower

32 CPUs

Fastest

Fprintln

18.35 ns/op

Slowest

Print

112.33 ns/op · 6.1x slower

Performance Comparison (lower is better)
CPU:
#1

Fprintln #

Fastest

Uses fmt.Fprintln to write a string followed by a newline directly to io.Discard, bypassing stdout entirely.

CPU Scaling (lower is better)
const s = "Hello, World!"

// Fprintln writes to an io.Writer with a trailing newline.
func BenchmarkFprintln_run(b *testing.B) {
	for i := 0; i < b.N; i++ {
		fmt.Fprintln(io.Discard, s)
	}
}
1 CPU
1.1xfaster(6%)thanFprint
1.2xfaster(21%)thanFprintf
6.4xfaster(544%)thanPrint
6.3xfaster(534%)thanPrintf
6.2xfaster(521%)thanPrintln
32 CPUs
1xfaster(4%)thanFprint
1.2xfaster(20%)thanFprintf
6.1xfaster(512%)thanPrint
6.1xfaster(512%)thanPrintf
6.1xfaster(507%)thanPrintln
#2

Fprint #

Uses fmt.Fprint to write a string directly to io.Discard, bypassing stdout entirely.

CPU Scaling (lower is better)
const s = "Hello, World!"

// Fprint writes to an io.Writer.
func BenchmarkFprint_run(b *testing.B) {
	for i := 0; i < b.N; i++ {
		fmt.Fprint(io.Discard, s)
	}
}
1 CPU
1.1xfaster(14%)thanFprintf
1.1xslower(6%)thanFprintln
6.1xfaster(507%)thanPrint
6xfaster(497%)thanPrintf
5.8xfaster(485%)thanPrintln
32 CPUs
1.2xfaster(16%)thanFprintf
1xslower(4%)thanFprintln
5.9xfaster(489%)thanPrint
5.9xfaster(488%)thanPrintf
5.8xfaster(484%)thanPrintln
#3

Fprintf #

Uses fmt.Fprintf with a %s verb to write a formatted string directly to io.Discard, bypassing stdout entirely.

CPU Scaling (lower is better)
const s = "Hello, World!"

// Fprintf writes a formatted string to an io.Writer.
func BenchmarkFprintf_run(b *testing.B) {
	for i := 0; i < b.N; i++ {
		fmt.Fprintf(io.Discard, "%s\n", s)
	}
}
1 CPU
1.1xslower(14%)thanFprint
1.2xslower(21%)thanFprintln
5.3xfaster(434%)thanPrint
5.3xfaster(425%)thanPrintf
5.1xfaster(414%)thanPrintln
32 CPUs
1.2xslower(16%)thanFprint
1.2xslower(20%)thanFprintln
5.1xfaster(409%)thanPrint
5.1xfaster(409%)thanPrintf
5xfaster(405%)thanPrintln
#4

Println #

Uses fmt.Println to write a string followed by a newline to stdout (redirected to /dev/null).

CPU Scaling (lower is better)
const s = "Hello, World!"

// Println writes to stdout with a trailing newline.
func BenchmarkPrintln_run(b *testing.B) {
	os.Stdout, _ = os.Open(os.DevNull)

	for i := 0; i < b.N; i++ {
		fmt.Println(s)
	}
}
1 CPU
5.8xslower(485%)thanFprint
5.1xslower(414%)thanFprintf
6.2xslower(521%)thanFprintln
1xfaster(4%)thanPrint
1xfaster(2%)thanPrintf
32 CPUs
5.8xslower(484%)thanFprint
5xslower(405%)thanFprintf
6.1xslower(507%)thanFprintln
1xfaster(1%)thanPrint
1xfaster(1%)thanPrintf
#5

Printf #

Uses fmt.Printf with a %s verb to write a formatted string to stdout (redirected to /dev/null).

CPU Scaling (lower is better)
const s = "Hello, World!"

// Printf writes a formatted string to stdout.
func BenchmarkPrintf_run(b *testing.B) {
	os.Stdout, _ = os.Open(os.DevNull)

	for i := 0; i < b.N; i++ {
		fmt.Printf("%s\n", s)
	}
}
1 CPU
6xslower(497%)thanFprint
5.3xslower(425%)thanFprintf
6.3xslower(534%)thanFprintln
1xfaster(2%)thanPrint
1xslower(2%)thanPrintln
32 CPUs
5.9xslower(488%)thanFprint
5.1xslower(409%)thanFprintf
6.1xslower(512%)thanFprintln
Same speed asPrint
1xslower(1%)thanPrintln
#6

Print #

Slowest

Uses fmt.Print to write a string to stdout (redirected to /dev/null).

CPU Scaling (lower is better)
const s = "Hello, World!"

// Print writes to stdout.
func BenchmarkPrint_run(b *testing.B) {
	os.Stdout, _ = os.Open(os.DevNull)

	for i := 0; i < b.N; i++ {
		fmt.Print(s)
	}
}
1 CPU
6.1xslower(507%)thanFprint
5.3xslower(434%)thanFprintf
6.4xslower(544%)thanFprintln
1xslower(2%)thanPrintf
1xslower(4%)thanPrintln
32 CPUs
5.9xslower(489%)thanFprint
5.1xslower(409%)thanFprintf
6.1xslower(512%)thanFprintln
Same speed asPrintf
1xslower(1%)thanPrintln

Contributors