iOS5系统API和5个开源库的JSON解析速度测试

 

iOS5新增了JSON解析的API,我们将其和其他五个开源的JSON解析库进行了解析速度的测试,下面是测试的结果和工程代码附件。

 

我们选择的测试对象包含下面的这几个框架,其中NSJSONSerialization是iOS5系统新增的JSON解析的API,需要iOS5的环境,如果您在更低的版本进行测试,应该屏蔽相应的代码调用。

 

- [SBJSON (json-framework)](http://code.google.com/p/json-framework/)

- [TouchJSON (from touchcode)](http://code.google.com/p/touchcode/)

- [YAJL (objective-C bindings)](http://github.com/gabriel/yajl-objc)

- [JSONKit](https://github.com/johnezang/JSONKit)

- [NextiveJson](https://github.com/nextive/NextiveJson)

-[NSJSONSerialization](http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946)

 

我们选择了四个包含json格式的数据的文件进行测试。每一个文件进行100的解析动作,对解析的时间进行比较。

 

工程包含以下的文件和框架:

 

测试时间间隔的的代码的宏定义如下,其中计算的次数和解析的代码由外部调用传入:

#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

 

将上面的数据整理成下面的图表:

 

测试的结果显示,系统的API的解析速度最快,我们在工程项目中选择使用,也是应用较为广泛的SBJSON的解析速度为倒数第二差,令我大跌眼镜。

与系统API较为接近的应该是JSONKit。

 

这里没有对API的开放接口和使用方式进行比较,若单纯基于以上解析速度的测试:

1:iOS5应该选择系统的API进行

2:不能使用系统API的应该选择JSONKit

 

程序附件:

http://arthurchen.blog.51cto.com/attachment/201111/2483760_1322144362.zip

本文word文档:

http://arthurchen.blog.51cto.com/attachment/201111/2483760_1322144787.zip

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值