2014-10-04 51 views
4

我已经做了这个测试项目https://github.com/danielpetroianu/FileDeserializeBenchmarking,看看我能从应用包中读取文件并反序列化它的最快方式。ObjC Plist文件读取速度比JSON快吗?

我很惊讶地发现Plist文件的读取速度比JSON快。由于JSON文件尺寸较小,我预计它会更快。

Xcode是否对构建时的Plist文件进行了一些优化? 我做错了什么导致JSON反序列化需要更多时间吗?

+2

你检查过的.plist文件是否被转换成二进制.plists在构建时?检查生成的应用程序包内部。 – Tommy 2014-10-07 17:04:53

+4

查看http://opensource.apple.com/source/CF/CF-550/CFBinaryPList.c关于二进制plists是如何实现的。您可能会以比text json更快的速度读取(理智的)二进制格式的文件。指定段的长度而不是寻找分隔符,在创建时进行更多验证等 – KirkSpaziani 2014-10-07 17:10:05

回答

12

由于JSON文件尺寸较小,我预计它会更快。

你没有理由相信这一点。有很多因素比文件大小重要得多。

是否有一些优化,Xcode中上的plist文件确实在构建时

是。如果它们在资源包中,它会将它们编译为Plist二进制格式,在某些情况下(可能所有情况都是如此),读取和解析比文本格式更快。这是在CopyPlistFile构建阶段完成的。

建成后,这里就是他们的样子:

-rwxr-xr-x 1 rnapier wheel  39556 Oct 7 13:06 FileDeserializeBenchmarking 
-rw-r--r-- 1 rnapier wheel  967 Oct 7 13:06 Info.plist 
-rw-r--r-- 1 rnapier wheel   8 Oct 7 13:06 PkgInfo 
-rw-r--r-- 1 rnapier wheel  111 Oct 7 13:06 data_dictionary_root_1.json 
-rw-r--r-- 1 rnapier wheel  110 Oct 7 13:06 data_dictionary_root_1.plist 
-rw-r--r-- 1 rnapier wheel  982 Oct 7 13:06 data_dictionary_root_10.json 
-rw-r--r-- 1 rnapier wheel  441 Oct 7 13:06 data_dictionary_root_10.plist 
-rw-r--r-- 1 rnapier wheel  9661 Oct 7 13:06 data_dictionary_root_100.json 
-rw-r--r-- 1 rnapier wheel  4219 Oct 7 13:06 data_dictionary_root_100.plist 
-rw-r--r-- 1 rnapier wheel  96488 Oct 7 13:06 data_dictionary_root_1000.json 
-rw-r--r-- 1 rnapier wheel  37730 Oct 7 13:06 data_dictionary_root_1000.plist 
-rw-r--r-- 1 rnapier wheel 965597 Oct 7 13:06 data_dictionary_root_10000.json 
-rw-r--r-- 1 rnapier wheel 233071 Oct 7 13:06 data_dictionary_root_10000.plist 
-rw-r--r-- 1 rnapier wheel 11655908 Oct 7 13:06 data_dictionary_root_100000.json 
-rw-r--r-- 1 rnapier wheel 3343077 Oct 7 13:06 data_dictionary_root_100000.plist 

$ file *.plist 
Info.plist:      Apple binary property list 
data_dictionary_root_1.plist:  Apple binary property list 
data_dictionary_root_10.plist:  Apple binary property list 
data_dictionary_root_100.plist: Apple binary property list 
data_dictionary_root_1000.plist: Apple binary property list 
data_dictionary_root_10000.plist: Apple binary property list 
data_dictionary_root_100000.plist: Apple binary property list