Re: Which is more efficient/faster: strint to integer conversion

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Justin Rogers (Justin_at_games4dotnet.com)
Date: 10/14/04


Date: Thu, 14 Oct 2004 14:58:34 -0700

Moving forward you'll want to use int.TryParse which avoids an
exception on invalid input if you are really looking for optimal speed.
Parse will still run faster in the situation where there are no parsing
errors, but you'll have to guarantee that only about 1 in 150 inputs
are invalid before you'll see any results from this.

Do note that the code-paths moving forward may be diverging.
There are separate, yet nearly identical methods for exceptional
parsing and boolean success parsing... If you really need speed I
might suggest taking a look at the number utilities collection I put
together. They are heavily tailored to my own requirements and
aren't culture sensitive, so they might not work for you.

http://weblogs.asp.net/justin_rogers/articles/104504.aspx

-- 
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message 
news:MPG.1bd8fe771ab994e098b6e1@msnews.microsoft.com...
> JaD <JustADeveloper@invalid> wrote:
>> Question: Convert string to integer efficiency...
>>
>> string s = "1";
>>
>> Choice 1:
>> int i = Convert.ToInt32(s);
>>
>> Choice 2:
>> int i = int.Parse(s);
>
> The first is just a call to the second, after a nullity check - so the
> second is very slightly faster, but not a lot. (If you want it to fail
> with nulls, use the second. If you want it to return 0 for nulls, use
> the first.)
>
> -- 
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too 


Relevant Pages

  • Re: function to convert degree (hour), minute, seconds string to integer
    ... reinvent it again, but just in case... ... open-source, function out there that takes a string in the form of ... something that throws an exception if hh, mm, or ss is out of range, or ... *all* invalid input. ...
    (comp.lang.python)
  • Re: Input Types
    ... Jeff Epler wrote: ... > exception that is caught or the error prompt string. ...
    (comp.lang.python)
  • Ian Chappell on SA catching
    ... Says that catchers are not moving their feet into position ... One exception he notes is Smith ...
    (rec.sport.cricket)
  • Re: newbie - jdbc Problem
    ... Try moving the con.setAutoCommit; to be the first database statement ... Is it an exception or a compile time error ... >> If it's an exception can you post the details. ...
    (comp.lang.java.programmer)
  • Re: OMG: Texting distracts teen drivers
    ... messages) cannot be placed or received, and calls in progress ... disconnected if the phone is moving at more than 5 mph. ... exception to this would be calls to emergency services. ...
    (misc.transport.road)