天天看點

iOS5系統API和5個開源庫的JSON解析速度測試

iOS5新增了JSON解析的API,我們将其和其他五個開源的JSON解析庫進行了解析速度的測試,下面是測試的結果和工程代碼附件。

我們選擇的測試對象包含下面的這幾個架構,其中NSJSONSerialization是iOS5系統新增的JSON解析的API,需要iOS5的環境,如果您在更低的版本進行測試,應該屏蔽相應的代碼調用。

我們選擇了四個包含json格式的資料的檔案進行測試。每一個檔案進行100的解析動作,對解析的時間進行比較。

工程包含以下的檔案和架構:

iOS5系統API和5個開源庫的JSON解析速度測試

測試時間間隔的的代碼的宏定義如下,其中計算的次數和解析的代碼由外部調用傳入:

#define RunWithCount(count, description, expr) \

do { \

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent(); \

for(NSInteger i = 0; i < count; i++) { \

         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; \

         expr; \

         [pool release]; \

} \

\

CFTimeInterval took = CFAbsoluteTimeGetCurrent() - start; \

NSLog(@"%@ %0.3f", description, took); \

} while (0)

這是外面調用的代碼,設定讀取的json檔案和計算的次數,每一個函數在進行對應架構API的解析代碼:

         JSONTest *test = [[JSONTest alloc] init];

         NSInteger count = 100;

        [test runWithResourceName:@"twitter_public.json" count:count];

         [test runWithResourceName:@"lastfm.json" count:count];

         [test runWithResourceName:@"delicious_popular.json" count:count];

         [test runWithResourceName:@"yelp.json" count:count];

我們的測試的環境是Xcode 4.2和iOS5,計算次數是100次,這是計算的結果Log:

2011-11-24 14:48:59.441 JSONPerfTest[9716:207] SBJSON-twitter_public.json 0.335

2011-11-24 14:48:59.625 JSONPerfTest[9716:207] YAJL-twitter_public.json 0.183

2011-11-24 14:49:00.095 JSONPerfTest[9716:207] TouchJSON-twitter_public.json 0.469

2011-11-24 14:49:00.226 JSONPerfTest[9716:207] JSONKit-twitter_public.json 0.130

2011-11-24 14:49:00.390 JSONPerfTest[9716:207] NextiveJson-twitter_public.json 0.164

2011-11-24 14:49:00.504 JSONPerfTest[9716:207] NSJSONSerialization-twitter_public.json 0.113

2011-11-24 14:49:01.196 JSONPerfTest[9716:207] SBJSON-lastfm.json 0.691

2011-11-24 14:49:01.516 JSONPerfTest[9716:207] YAJL-lastfm.json 0.320

2011-11-24 14:49:02.367 JSONPerfTest[9716:207] TouchJSON-lastfm.json 0.850

2011-11-24 14:49:02.580 JSONPerfTest[9716:207] JSONKit-lastfm.json 0.212

2011-11-24 14:49:02.861 JSONPerfTest[9716:207] NextiveJson-lastfm.json 0.280

2011-11-24 14:49:03.039 JSONPerfTest[9716:207] NSJSONSerialization-lastfm.json 0.177

2011-11-24 14:49:03.546 JSONPerfTest[9716:207] SBJSON-delicious_popular.json 0.506

2011-11-24 14:49:03.787 JSONPerfTest[9716:207] YAJL-delicious_popular.json 0.240

2011-11-24 14:49:04.460 JSONPerfTest[9716:207] TouchJSON-delicious_popular.json 0.672

2011-11-24 14:49:04.668 JSONPerfTest[9716:207] JSONKit-delicious_popular.json 0.207

2011-11-24 14:49:04.904 JSONPerfTest[9716:207] NextiveJson-delicious_popular.json 0.234

2011-11-24 14:49:05.072 JSONPerfTest[9716:207] NSJSONSerialization-delicious_popular.json 0.168

2011-11-24 14:49:05.434 JSONPerfTest[9716:207] SBJSON-yelp.json 0.361

2011-11-24 14:49:05.633 JSONPerfTest[9716:207] YAJL-yelp.json 0.198

2011-11-24 14:49:06.154 JSONPerfTest[9716:207] TouchJSON-yelp.json 0.519

2011-11-24 14:49:06.310 JSONPerfTest[9716:207] JSONKit-yelp.json 0.155

2011-11-24 14:49:06.497 JSONPerfTest[9716:207] NextiveJson-yelp.json 0.186

2011-11-24 14:49:06.637 JSONPerfTest[9716:207] NSJSONSerialization-yelp.json 0.140

将上面的資料整理成下面的圖表:

iOS5系統API和5個開源庫的JSON解析速度測試

測試的結果顯示,系統的API的解析速度最快,我們在工程項目中選擇使用,也是應用較為廣泛的SBJSON的解析速度為倒數第二差,令我大跌眼鏡。

與系統API較為接近的應該是JSONKit。

這裡沒有對API的開放接口和使用方式進行比較,若單純基于以上解析速度的測試:

1:iOS5應該選擇系統的API進行

2:不能使用系統API的應該選擇JSONKit

程式附件:

<a href="http://arthurchen.blog.51cto.com/attachment/201111/2483760_1322144362.zip">http://arthurchen.blog.51cto.com/attachment/201111/2483760_1322144362.zip</a>

本文word文檔:

<a href="http://arthurchen.blog.51cto.com/attachment/201111/2483760_1322144787.zip">http://arthurchen.blog.51cto.com/attachment/201111/2483760_1322144787.zip</a>