All Benchmarks

Go Benchmark

Int to String

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 atCPUs

1 CPU

Fastest

Strconv Itoa

11.87 ns/op

Slowest

Fmt Sprint

36.05 ns/op · 3.0x slower

32 CPUs

Fastest

Strconv Itoa

12.29 ns/op

Slowest

Fmt Sprintf

37.43 ns/op · 3.0x slower

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 (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
3xfaster(204%)thanFmt Sprint
2.9xfaster(191%)thanFmt Sprintf
1xfaster(2%)thanStrconv Format Int
32 CPUs
3xfaster(197%)thanFmt Sprint
3xfaster(205%)thanFmt Sprintf
1xfaster(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 (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
3xfaster(199%)thanFmt Sprint
2.9xfaster(186%)thanFmt Sprintf
1xslower(2%)thanStrconv Itoa
32 CPUs
2.9xfaster(193%)thanFmt Sprint
3xfaster(201%)thanFmt Sprintf
1xslower(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 (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
1xfaster(4%)thanFmt Sprint
2.9xslower(186%)thanStrconv Format Int
2.9xslower(191%)thanStrconv Itoa
32 CPUs
1xslower(2%)thanFmt Sprint
3xslower(201%)thanStrconv Format Int
3xslower(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 (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
1xslower(4%)thanFmt Sprintf
3xslower(199%)thanStrconv Format Int
3xslower(204%)thanStrconv Itoa
32 CPUs
1xfaster(2%)thanFmt Sprintf
2.9xslower(193%)thanStrconv Format Int
3xslower(197%)thanStrconv Itoa

Contributors