I'm going to do something unfair to make a point: int.TryParse versus DWC.NumberUtilities.

Note this is a completely unfair test because Whidbey is in Beta and all, but I'll throw it out there anyway.  I went ahead and tested the original TryParseInt method (the completely unoptimized version that has been deprecated in the utility), against TryParseInt32 (the fully optimized version), and int.TryParse.  Note that all of the conversions were valid conversions, and full validity checking was in place for all three methods (aka, make sure the value that goes in is the value coming back out).  Fortunately, TryParse is only 7x slower than TryParseInt32 now, which is a step in the right direction, and it doesn't throw exceptions, nor require a cast from double in the case you used double.TryParse.  I'm definitely glad they added the new TryParse methods because they give you one liner options that you would have needed a bit of code for before.

C:\Projects\CSharp\Performance\NumberUtilities>TryParseInt32
Preallocate strings
Preallocation complete
TryParseInt: 00:00:01.0114544
TryParseInt32: 00:00:00.3404896
int.TryParse: 00:00:02.5336432

// Old options without DWC.NumberUtilities or the new BCL TryParse methods
// The old try method
try { int.Parse(); } catch {} 

// The double TryParse method with range checking
double.TryParse(); if (retVal < int.MinValue || retVal > int.MaxValue) {}

Published Monday, April 26, 2004 7:13 PM by Justin Rogers

Comments

Tuesday, April 27, 2004 12:58 AM by Jesse Ezell

# re: I'm going to do something unfair to make a point: int.TryParse versus DWC.NumberUtilities.

Yah, too bad they left them out of the first release... which is really strange, considering that they remembered to put in double.tryparse...
Wednesday, June 09, 2004 9:27 PM by Brian Grunkemeyer

# re: I'm going to do something unfair to make a point: int.TryParse versus DWC.NumberUtilities.

The need for these methods wasn't realized until too late in the ship cycle. We added one TryParse method because it was causing some noticable delays in an important perf scenario, then we got around to doing the rest significantly later.

I don't imagine your TryParseInt32 is culturally correct. That's probably a significant portion of why Int32.TryParse is slower.
Wednesday, June 09, 2004 10:25 PM by Justin Rogers

# re: I'm going to do something unfair to make a point: int.TryParse versus DWC.NumberUtilities.

No, but even the culturally correct version that I wrote is 3x faster. So that wasn't the significant portion at all. The significant portion is the single code-path for parsing all numeric types that does a bunch of extra processing logic in cases where said logic is unnecessary. Having the new TryParse methods around in Whidbey is quite the good thing as I've noted, but I'll still use my own libraries for string and numeric processing since they are quite the bit faster.

Leave a Comment

(required) 
(required) 
(optional)
(required)