All Benchmarks

Printing — Go Benchmark

Comparing the performance of different fmt printing functions in Go.

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 at
CPUs

1 CPU

Fastest

Fprintln

Slowest

Print

32 CPUs

Fastest

Fprintln

Slowest

Print
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 — Fprintln (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.1×faster(6%)thanFprint
1.2×faster(21%)thanFprintf
6.4×faster(544%)thanPrint
6.3×faster(534%)thanPrintf
6.2×faster(521%)thanPrintln
32 CPUs
1×faster(4%)thanFprint
1.2×faster(20%)thanFprintf
6.1×faster(512%)thanPrint
6.1×faster(512%)thanPrintf
6.1×faster(507%)thanPrintln
2

Fprint

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

CPU Scaling — Fprint (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.1×faster(14%)thanFprintf
1.1×slower(6%)thanFprintln
6.1×faster(507%)thanPrint
6×faster(497%)thanPrintf
5.8×faster(485%)thanPrintln
32 CPUs
1.2×faster(16%)thanFprintf
1×slower(4%)thanFprintln
5.9×faster(489%)thanPrint
5.9×faster(488%)thanPrintf
5.8×faster(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 — Fprintf (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.1×slower(14%)thanFprint
1.2×slower(21%)thanFprintln
5.3×faster(434%)thanPrint
5.3×faster(425%)thanPrintf
5.1×faster(414%)thanPrintln
32 CPUs
1.2×slower(16%)thanFprint
1.2×slower(20%)thanFprintln
5.1×faster(409%)thanPrint
5.1×faster(409%)thanPrintf
5×faster(405%)thanPrintln
4

Println

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

CPU Scaling — Println (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.8×slower(485%)thanFprint
5.1×slower(414%)thanFprintf
6.2×slower(521%)thanFprintln
1×faster(4%)thanPrint
1×faster(2%)thanPrintf
32 CPUs
5.8×slower(484%)thanFprint
5×slower(405%)thanFprintf
6.1×slower(507%)thanFprintln
1×faster(1%)thanPrint
1×faster(1%)thanPrintf
5

Printf

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

CPU Scaling — Printf (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
6×slower(497%)thanFprint
5.3×slower(425%)thanFprintf
6.3×slower(534%)thanFprintln
1×faster(2%)thanPrint
1×slower(2%)thanPrintln
32 CPUs
5.9×slower(488%)thanFprint
5.1×slower(409%)thanFprintf
6.1×slower(512%)thanFprintln
Same speed asPrint
1×slower(1%)thanPrintln
6

Print

Slowest

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

CPU Scaling — Print (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.1×slower(507%)thanFprint
5.3×slower(434%)thanFprintf
6.4×slower(544%)thanFprintln
1×slower(2%)thanPrintf
1×slower(4%)thanPrintln
32 CPUs
5.9×slower(489%)thanFprint
5.1×slower(409%)thanFprintf
6.1×slower(512%)thanFprintln
Same speed asPrintf
1×slower(1%)thanPrintln

Contributors