Go Benchmark
Int to String
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
32 CPUs
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.
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.
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.
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