Int to String — Go Benchmark
Compare integer-to-string conversion methods in Go's standard library.
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.
1 CPU
Fastest
Strconv ItoaSlowest
Fmt Sprint32 CPUs
Fastest
Strconv ItoaSlowest
Fmt SprintfStrconv Itoa
FastestConverts 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.
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.
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.
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.
Contributors