Re: 30x faster JSON date parsing
Re: 30x faster JSON date parsing
- Subject: Re: 30x faster JSON date parsing
- From: Jean-Daniel Dupas <email@hidden>
- Date: Mon, 09 Sep 2013 19:05:56 +0200
Le 9 sept. 2013 à 18:11, Jens Alfke <email@hidden> a écrit :
> Parsing dates from strings can be surprisingly expensive — more than once I’ve seen it show up as the primary hot-spot in code that reads files or parses network data. NSDateFormatter is very flexible, but you pay for that in speed. If you only need to parse a single simple date/time format you can do a lot better.
>
> A couple of days ago Anrurag Mishra wrote a blog post[1] demonstrating that you can get about 14x faster performance (yes, 14 *times* faster) by calling SQLite’s built-in strftime function. (Actually, my experience is that it’s 30x faster, but I was probably running on different hardware. YMMV.) His code simply compiles and runs a SQL “SELECT” statement that calls strftime — I decided to do better than that by extracting the C code from SQLite that does the actual parsing, and making a direct API for it. That about doubles performance again. Here are some benchmark results (run on a 2012 MacBook Pro):
>
> [fg160,160,160;16:34:40.488| [fg0,128,0;[;NSDateFormatter took 26.97 µsec
> [fg160,160,160;16:34:42.709| [fg0,128,0;[;sqlite3 strftime took 0.89 µsec (30x)
> [fg160,160,160;16:34:46.788| [fg0,128,0;[;-dateWithJSONObject took 0.68 µsec (40x)
> [fg160,160,160;16:34:48.649| [fg0,128,0;[;CBLParseDate took 0.47 µsec (58x)
>
> (Here CBLParseDate is the raw function that returns a UNIX timestamp, and -dateWithJSONObject is a wrapper method that returns the same time as an NSDate object, which of course has extra overhead.)
>
> Now, the tradeoff is that the optimized code parses only ISO 8601 dates — this is the informal standard used with JSON, so it’s become really common. It looks like “2013-09-09T17:52:12Z”. It’s certainly possible to munge this code to parse a slightly different format, but you’re on your own there!
>
> You can grab my code (which is just a hack-and-slash of Richard Hipp’s SQLite code) from the Couchbase Lite repo[2]. It has no dependencies on anything but the C standard library, so it should be easy to drop into your own projects.
>
Isn't it possible to parse it using the libc function strftime ? How does it compare to the other solutions ?
-- Jean-Daniel
_______________________________________________
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