Re: Which is more efficient/faster: strint to integer conversion
From: Justin Rogers (Justin_at_games4dotnet.com)
Date: 10/14/04
- Next message: denz: "Re: inspecting the number of function calls within a particular fu"
- Previous message: Jon Skeet [C# MVP]: "Re: Which is more efficient/faster: strint to integer conversion"
- In reply to: Jon Skeet [C# MVP]: "Re: Which is more efficient/faster: strint to integer conversion"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: denz: "Re: inspecting the number of function calls within a particular fu"
- Previous message: Jon Skeet [C# MVP]: "Re: Which is more efficient/faster: strint to integer conversion"
- In reply to: Jon Skeet [C# MVP]: "Re: Which is more efficient/faster: strint to integer conversion"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|