Re: Home-brewed code is 100X faster than -[NSScanner scanDecimal:] ??
Re: Home-brewed code is 100X faster than -[NSScanner scanDecimal:] ??
- Subject: Re: Home-brewed code is 100X faster than -[NSScanner scanDecimal:] ??
- From: glenn andreas <email@hidden>
- Date: Thu, 03 Feb 2011 10:56:05 -0600
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 email@hidden
The most merciful thing in the world ... is the inability of the human mind to correlate all its contents - HPL
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden