On Feb 3, 2011, at 10:35 AM, Jerry Krinock wrote:

> -[NSScanner scanDecimal:] takes an average of 4 milliseconds to scan a short 
> string of decimal digits, which means tens of seconds for thousands of scans, 
> which is unacceptable for my application.  Also, excessive memory allocations 
> require a local autorelease pool around each invocation.
> 
> Surprisingly, I was able to fix both problem by replacing -scanDecimal: with 
> a rather bone-headed home-brew implementation, using 
> -scanCharactersFromSet:intoString: instead.  The home-brew implementation 
> runs 100 to 150 times faster.  How can this be?
> 

Your scanner doesn't correctly handle all valid decimals.

First, you don't handle scientific notation.

More importantly, you do not handle the full range of NSDecimal which is 
documented to be 38 _DECIMAL_ digits.  This is important because scanning in 
0.1 into a double will result in loss of accuracy (since 0.1 can't be expressed 
exactly in binary), where as NSDecimal will be able to handle it correctly.  
For that matter, 52 bits of mantissa on a double is only approximately 16 
decimal digits (no where near as close as the 38 decimal digits of NSDecimal).




Glenn Andreas                      gandr...@gandreas.com 
The most merciful thing in the world ... is the inability of the human mind to 
correlate all its contents - HPL

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to