All Benchmarks

Int to String — Go Benchmark

Compare integer-to-string conversion methods in Go's standard library.

formattingconversionstringintstringify

Formatting an integer to a string is one of the most common operations in Go. This benchmark compares the performance of strconv.Itoa, strconv.FormatInt, fmt.Sprint, and fmt.Sprintf when converting standard integers. Since the fmt package relies heavily on reflection and format string parsing, it carries significantly more overhead than strconv functions that contain specialized routines for number building.

linux/amd64AMD Ryzen 9 9950X3D 16-Core Processorbenchmarks/int-to-string
Compare at
CPUs

1 CPU

32 CPUs

Performance Comparison (lower is better)
CPU:
1

Strconv Itoa

Fastest

Converts an int directly to a string. strconv.Itoa internally casts the int to int64 and calls strconv.FormatInt. This involves small numbers optimization and efficient digit-by-digit division without reflection.

CPU Scaling — Strconv Itoa (lower is better)
func BenchmarkStrconvItoa_convert(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sink = strconv.Itoa(testInts[i&1023]) // Use bitwise AND for fast modulo 1024
	}
}
1 CPU
3×faster(204%)thanFmt Sprint
2.9×faster(191%)thanFmt Sprintf
1×faster(2%)thanStrconv Format Int
32 CPUs
3×faster(197%)thanFmt Sprint
3×faster(205%)thanFmt Sprintf
1×faster(1%)thanStrconv Format Int
2

Strconv Format Int

Converts a given int64 directly to a string, allowing bases from 2 to 36. Its performance is virtually identical to strconv.Itoa when used with base 10 for int values, because Itoa wraps it immediately.

CPU Scaling — Strconv Format Int (lower is better)
func BenchmarkStrconvFormatInt_convert(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sink = strconv.FormatInt(int64(testInts[i&1023]), 10) // Use bitwise AND for fast modulo 1024
	}
}
1 CPU
3×faster(199%)thanFmt Sprint
2.9×faster(186%)thanFmt Sprintf
1×slower(2%)thanStrconv Itoa
32 CPUs
2.9×faster(193%)thanFmt Sprint
3×faster(201%)thanFmt Sprintf
1×slower(1%)thanStrconv Itoa
3

Fmt Sprintf

Slowest (32 CPUs)

A general-purpose formatter using fmt.Sprintf("%d", value). It scans the format string, creates an internal reflection object to represent the interface{}, and performs the conversion. This approach is highly flexible but significantly slower and more alloc-heavy.

CPU Scaling — Fmt Sprintf (lower is better)
func BenchmarkFmtSprintf_convert(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sink = fmt.Sprintf("%d", testInts[i&1023]) // Use bitwise AND for fast modulo 1024
	}
}
1 CPU
1×faster(4%)thanFmt Sprint
2.9×slower(186%)thanStrconv Format Int
2.9×slower(191%)thanStrconv Itoa
32 CPUs
1×slower(2%)thanFmt Sprint
3×slower(201%)thanStrconv Format Int
3×slower(205%)thanStrconv Itoa
4

Fmt Sprint

Slowest (1 CPU)

A general-purpose formatter without format strings using fmt.Sprint(value). It avoids parsing a formatting string like "%d", but still incurs the overhead of converting the argument to interface{} and looking up reflection handlers.

CPU Scaling — Fmt Sprint (lower is better)
func BenchmarkFmtSprint_convert(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sink = fmt.Sprint(testInts[i&1023]) // Use bitwise AND for fast modulo 1024
	}
}
1 CPU
1×slower(4%)thanFmt Sprintf
3×slower(199%)thanStrconv Format Int
3×slower(204%)thanStrconv Itoa
32 CPUs
1×faster(2%)thanFmt Sprintf
2.9×slower(193%)thanStrconv Format Int
3×slower(197%)thanStrconv Itoa

Contributors