Strconv.Itoa vs Strconv.FormatInt vs fmt.Sprintf which is good to Convert Int to String In Golang
Tags: beginners, programming, tutorial, go
Published: 2022-11-07 14:44:21

In this article we are going to check the performance of these methods, which method is fast and which is the slowest for converting Int to String in Golang, and how we are going to do that, we are going to check the benchmark results of each method for two values one is low value and second is high value, 45(small) and 8976497664(big) like something.

Below is the benchmark code and that code we've all three ways into their separate functions.

Code is stored in main_test.go file

```go
package main

import (
    "fmt"
    "strconv"
    "testing"
)

var smallInt = 40
var bigInt = 999999999999999

func Itoa(s int) string {
    i := strconv.Itoa(s)
    return i
}

func FormatInt(s int) string {
    i := strconv.FormatInt(int64(s), 10)
    return i
}

func Sprintf(s int) string {
    i := fmt.Sprintf("%d", s)
    return i
}

func BenchmarkItoa(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Itoa(smallInt)
    }
}

func BenchmarkFormatInt(b *testing.B) {
    for i := 0; i < b.N; i++ {
        FormatInt(smallInt)
    }
}

func BenchmarkSprintf(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Sprintf(smallInt)
    }
}

func BenchmarkItoaBig(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Itoa(bigInt)
    }
}

func BenchmarkFormatIntBig(b *testing.B) {
    for i := 0; i < b.N; i++ {
        FormatInt(bigInt)
    }
}

func BenchmarkSprintfBig(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Sprintf(bigInt)
    }
}
```

We have _smallInt_ and _bigInt_ variable which have their corresponding values, as well we have three function as mentioned above _Itoa_, _FormatInt_, and _Sprintf_ in which we are just trying to convert provided int value to string and returning back the _string_.
If you look at the code we have two functions for each Benchmark, because we want to test these functions for small and big values.

Now let's see the results of these outputs and you'll know which works faster compared to other.

![int to string golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zxsgwevyes7g2b483jr.png)

As you can see _fmt.Sprint_ is the slowest way to convert small and big int to string, and on the other hand _strconv.FormatInt_ is the fastest way to convert a int to string , _strconv,Itoa_ is the second fastest way almost near to _strconv.FormatInt_.

So hence prove that for either small or big always use _strconv.FormatInt_ or _strconv.Itoa_, instead of using _fmt.Sprintf_.

Now one thing comes in mind why _fmt.Sprintf_ is not fast like others, because _fmt.Sprintf()_ uses the reflect package and reflect package works on runtime which makes it slower than _strconv.Itoa()_ and _strconv.FormatInt()_ functions


_This article is originally posted on [programmingeeksclub.com](https://programmingeeksclub.com/how-to-convert-int-to-string-in-golang/)_

_My Personal Blogging Website : [Programming Geeks Club](https://programmingeeksclub.com/)_
_My Facebook Page : [Programming Geeks Club](https://www.facebook.com/profile.php?id=100086258693659)_
_My Telegram Channel : [Programming Geeks Club](https://t.me/dpgcl)_
_My Twitter Account : [Kuldeep Singh](https://twitter.com/kusinghofficial)_
_My Youtube Channel: [Programming Geeks Club](https://youtube.com/@programmingeeksclub)_