Many web services choose to return dates in something other than a unix timestamp (unfortunately).
Recently I set out trying to identify performance bottlenecks with a large import operation in our iOS app. Having tweaked most of the variables, I was surprised to find out that date parsing was actually one of the biggest bottlenecks. The date parsing was done using NSDateFormatter using an ISO-8601 formatted date that looked like this:
That looks simple enough. We had the NSDateFormatter’s format setup with a timezone of +0 GMT, and everything was great, expect parsing dates like this was consuming around 20% of the entire import operation. To provide some context, we were testing the import performance by importing roughly 250,000 objects into a SQLite database, and each object had 4 dates associated with it. That meant that we were dealing with a million dates, and parsing a million dates can get expensive.
Almost all of that time was being spent inside NSDateFormatter’s dateFromString: method, so there was not much we could do to optimize things ourselves.
The main goal was to get a unix timestamp from the ISO date. Luckily for us, SQLite is quite good at parsing some ISO-8601 dates and works blazingly fast. Here’s how SQLite can parse the above date:
sqlite> SELECT strftime("%s", "2013-09-07T23:45:00Z"); 1378597500
Let’s convert this to use SQLite’s C library.
sqlite3_stmt *statement = NULL; sqlite3_prepare_v2(db, "SELECT strftime('%s', ?);", -1, &statement, NULL); sqlite3_bind_text(statement, 1, [dateString UTF8String], -1, SQLITE_STATIC); sqlite3_step(statement); sqlite3_int64 interval = sqlite3_column_int64(statement, 0); NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
Looks ugly, but we’re trying to solve a genuine problem here. So, how did this do in a performance run against NSDateFormatter? About 1400% faster. To parse a million randomly generated dates on an iPhone 5 running iOS 7, NSDateFormatter took a whooping 106.27 seconds, while the SQLite version took just 7.02 seconds.
If you’re curious as to what SQLite is doing under the hood, checkout the date related code in SQLite at http://www.sqlite.org/src/doc/trunk/src/date.c. It mentions that the conversion algorithms it is using are from a book named Astronomical Algorithms by Jean Meeus.
- satanicboomboomhead likes this
- jasonconnery likes this
- to-the-two reblogged this from vombat
- ajithrnayak likes this
- chandrahasan1 likes this
- thought-palace reblogged this from vombat and added:
- wisesabre reblogged this from vombat
- patoroco reblogged this from vombat and added:
- dwlz likes this
- cfrs likes this
- wowitsphil reblogged this from vombat
- jerebearbolster reblogged this from vombat
- bramesposito likes this
- jamieomatthews likes this
- omg likes this
- dev-jac reblogged this from vombat and added:
- dev-jac likes this
- bendiken likes this
- evandrix likes this
- vombat posted this