13

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

Spread the love

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

This will examine and benchmark many techniques to determine in C# .Net: Fastest way to convert a string to an int.

 

I ended up working on a project once where I had to read a lot of integers in from a text file. By a lot I mean over 2 million. All those numbers, read in as strings, had to be converted to integers to perform mathematical calculations. When I profiled the code in Visual Studio, I saw that the Convert.ToInt() method was taking a long time (relatively speaking) compared to other parts of the code.

 

So that’s when this curious consultant started wondering in C# .Net: what is the fastest way to convert a string to an int?

 

The Set Up:

I wrote a C# Console application to benchmark 4 different 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 a specified array of strings all of which are integers.

2) It then uses 1 of 4 techniques to loop through the string array converting each string representation to the equivalent int, summing up for a total. The 4 techniques are as follows:

3)

#

Technique

Code Snippet

T0

Convert.ToInt32()

T1

Int32.TryParse()

T2

Int.Parse()

T3

A custom method which loops through and converts each character in the numeric string

 

4) The sum from adding up all the values (once converted) is displayed along with the time to verify the accuracy.

5) The string array is cleared out and deleted.

 

The code assumes all numbers:

  • are positive
  • do not have thousandths separators or decimal points
  • contain no scientific or other special notations
  • and all conversions will happen with no exception testing because I’m just wanting to test the raw speed of converting.

 

I realize some geeks will look at the code and say I’m not comparing apples-to-apples since the Int.TryParse() method under the hood tests for successful conversions, but it is a method commonly used so am including it.

 

The exe file was run on Windows 7 64-bit with 16 GB memory.

 

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

 

Ready! Set! Go!

Before starting, my hypothesis was that I expected it to be a virtual draw between T0 (using Convert.ToInt32() ) and T3 (converting each char to an int in the string).

 

Let’s see what happened on my machine.

 

All times are indicated in seconds.milliseconds format. Lower numbers indicate faster performance. Winner marked in green.

@ 21,474,836

@ 2,147,483

@ 250,000

@ 50,000

@ 1000

T0: Convert.ToInt32()

02.9016051

0.2808005

0.0312001

0.00

0.00

T1: Int32.TryParse()

02.8236050

0.2652005

0.0312000

0.0156000

0.00

T2: Int.Parse()

02.7612048

0.2652005

0.0312001

0.0156000

0.00

T3: Custom Method

00.1872004

0.0156001

0.00

0.00

0.00

 

Who opened up the can of whoop ass?!

It’s quite obvious the custom method T3 clearly wins completing in an order of magnitude of at least 10x faster than any of the native C# methods when doing significantly more than 50,000 conversions!!

 

Last Licks:

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. I’d use those methods for code readability.

 

If you end up working on a project like I have where you need to do a few million conversions, use the custom method for a 10x conversion performance gain. Knowing what you know now, why would you use anything else??

 

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

 

The Code:

 


Spread the love

David Lozinski