2014-10-04 50 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