Shantanu Gupta
Shantanu Gupta

Reputation: 21188

Which one is faster in processing and conversion int.Parse(), int.TryParse(), Convert.Int32()

Which one is faster, powerfull and reliable. And why ?

int.Parse()
int.TryParse()
Convert.ToInt32()

Upvotes: 5

Views: 3698

Answers (6)

Jaxidian
Jaxidian

Reputation: 13511

Go read this ("Performance Profiling Parse vs. TryParse vs. ConvertTo") for much add'l info.

If you are unsure if the string is parsable, then int.TryParse() will be MUCH faster than either of the others and catching the exceptions.

Upvotes: 9

SWeko
SWeko

Reputation: 30882

I find that the answer depends on the context.

If I'm converting (for example) a DataRow to an object, I'll have lots of Convert.ToXXX calls, so I would use Convert.ToInt32 because that is consistent with the other statements around that conversion.

In other situations, if I want to throw an exception when the string does not parse (fail-fast) I'll use int.Parse, because that throws the exception for me, and int.TryParse tends to generate uglier code (I've never been a fan of out parameters)

And if I just want to specify some default value if the string does not parse, I'll use int.TryParse, because otherwise, I'll have to handle the exception myself, and that is both expensive and ugly.

However, unless you are calling the parsing site billions of times, I would be surprised to see a discernible time difference between any of the three formats, so I would prefer a more readable piece of code, over a marginally faster variant.

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245399

Convert.Int32() calls Int32.Parse() with an extra check for null, so Int32.Parse() might be a tad faster. which is why Convert.Int32() will be faster (it catches the null before Int32.Parse() has to deal with it).

Int32.Parse() calls Number.ParseInt32() internally which throws Exceptions when a number can't be parsed.

Int32.TryParse() calls Number.TryParseInt32() internally which has similar code to Number.ParseInt32() but instead of throwing Exceptions it simply returns false...which introduces less overhead.

Considering all those variables, my guess would be that Int32.TryParse() will give you the fastest results for non-null values. If there's a chance the majority of the calls could contain nulls, I would say Convert.Int32() would perform better.

...all that brought to you by the power of .NET Reflector.

Upvotes: 5

Jürgen Steinblock
Jürgen Steinblock

Reputation: 31723

I personally use

int.Parse(...)

if I have a string as a source and

Convert.ToInt32(...)

if I have a valueType (double, decimal) as a source, because otherwise I had to cast it to strings and deal with the local culture.

Upvotes: 0

Ed B
Ed B

Reputation: 6054

Convert.ToInt32(string) calls Int32.Parse(string).

However, if you use Int32.Parse (or the equivalent int.Parse), you can specify globalization and formatting used when parsing.

Int.Parse will be faster since it doesn't do a try/catch.

Int.TryParse is more reliable and won't throw an error if you don't pass it a non-convertible value.

Upvotes: 0

JSBձոգչ
JSBձոգչ

Reputation: 41378

Both int.Parse and Convert.Int32 internally call int.TryParse, so performance differences between them should be trivial. TryParse is the most versatile in that it allows you to decide whether or not to throw an exception, but otherwise there is no difference between these methods.

Upvotes: 0

Related Questions