1

C# .Net: Fastest Way to Convert an Int to a String

Spread the love

C# .Net: fastest way to convert an int to a string

Here I am going to benchmark several techniques to find the fastest way to convert an int to a string.

 

All C# programmers have to do this at some point. After testing the fastest way to convert a string to an int (http://cc.davelozinski.com/c-sharp/fastest-way-to-convert-a-string-to-an-int), this curious consultant started wondering in C# .Net: what is the fastest way to convert an int to a string since we do it often enough?

 

The Set Up:

I wrote a C# Console application to test the following conversion techniques.

 

The code is written in Visual Studio 2012 targeting .Net Framework version 4.5 x64. The source code is available at the end of this blog so you can benchmark it on your own system if you wish.

 

In a nutshell, the code does the following:

1)      Creates an array of integers.

2)      Creates a similar sized array of strings which each int will be converted to. This is to remove time it would take to constantly reassigning a converted int to a single string object (memory allocation, etc) since strings are immutable.

3)      It then loops through the string array converting each string representation to the equivalent int, summing up for a total, using one of the techniques below:

#

Technique

Code Snippet

T0

ToString()

T1

Convert.ToString()

T2

String.Format

T3

Concatenate int and empty string

T4

Convert int to StringBuilder to string

 

4)      10 numbers from the string array are then displayed to verify they’re all the same

5)      The string and int arrays are cleared out and deleted.

 

The code assumes all conversions will happen with no exception testing because I’m just wanting to test the raw speed of converting.

 

The test was run for each technique over the following number of conversions:

  • 21,474,836
  • 2,147,483
  • 250,000
  • 50,000
  • 1,000
  • 100

 

The Runs:

Before starting, my hypothesis was that I expected technique T0 to be the fastest.

Let’s see what happened on my machine.

All times are indicated in seconds.milliseconds format. Lower numbers indicate faster performance. There are no points for second place:

 

@ 21,474,836

@ 2,147,483

@ 250,000

@ 50,000

@ 1000

@100

T0: .ToString()

03.8832221

0.3588007

0.0312001

0.0050002

0.00

0.00

T1: Convert.ToString()

04.1964073

0.3588006

0.0312000

0.0040003

0.00

0.00

T2: string.Format()

11.2258181

0.9512014

0.0780002

0.0120007

0.0010000

0.00

T3: int + String.Empty

07.8936139

0.7008012

0.0624001

0.0060004

0.00

0.00

T4: StringBuilder.ToString()

08.5384884

0.8980514

0.0840048

0.0090005

0.00

0.00

 

The Results:

I ran the test 3 times. They all pretty much came out the same, so I’m only displaying the results to the first run.

It’s a toss up between T0 and T1 techniques: T0 performed faster over 21 million conversions, but slightly slower at 2 million or less (I would love to know if there’s an application out there where 0.0000001 of a second makes a big performance difference). The other techniques took 2-3 times longer over a substantial number of conversions.

To my surprise, only T2 registered a time @ 1,000 conversions. I was expecting at least 2 or 3 of the techniques to register.

 

In Summary:

On my system, unless someone spots a flaw in my test code, it really makes no significant performance difference which method is used for anywhere up to a few thousand conversions. Use whatever method makes the most sense.

Over the course of a few million conversions, it looks like the native .ToString()  or Convert.ToString() methods are the best way to go.

Obviously you should test on your system before micro-optimizing this functionality for your .Net application.

Here’s the code so you can do so. 🙂

 

The Code:

 


Spread the love

David Lozinski