If you are considering to serialize a large amount of data to the disk, performance may become a concern to you. Python provides a serialization tool in the pickle module. There is also an optimized version called the cPickle. But how do they perform?
The data of concern to me is tabular data. In order to do a bake off, I have generated 50,000 records of sample data. The CSV representation is shown below:
seq, name, address, city, age, birthday 1000,John M. Doe,2147 Main St.,Middle Town 14,47,1985-05-15 1001,John N. Doe,2148 Main St.,Middle Town 15,48,1985-05-16 1002,John O. Doe,2149 Main St.,Middle Town 16,49,1985-05-17 1003,John P. Doe,2150 Main St.,Middle Town 17,50,1985-05-18 1004,John Q. Doe,2151 Main St.,Middle Town 18,51,1985-05-19 1005,John R. Doe,2152 Main St.,Middle Town 19,52,1985-05-20 1006,John S. Doe,2153 Main St.,Middle Town 20,53,1985-05-21 1007,John T. Doe,211 Main St.,Middle Town 21,1,1985-05-22 ...
Naturally, CSV is a contender for storing tabular data. (Indeed the data source I'm working with is in CSV format.) The two pickle modules produce identical data output. In addition, Python 2.6 also provides a JSON module that do the similar task as pickle but outputs a standard text based format. I included it in the comparison below.
First observation, CSV output the most compact data at 3MB. Pickle output is 40% larger at 4.2MB. JSON is somewhere in between. The speed? CSV is the winner among them all.
|Method||Load Time (ms)||File size (MB)|
Note that CSV reader create data items as string. In the sample data, two out of the six columns are integer fields. In order to do an apple-to-apple comparison I have another test that do integer conversion after loading such that the data loaded is identical to pickle's. This impacted the performance somewhat. But it is still more than twice as fast as the faster cPickle module. The standard library's JSON's performance trailing far behind, making it unsuitable for anything performance intensive. FYI, unlike the other modules, JSON's output is in unicode.
The test is done by Python 2.6 on Windows XP machine with 2.33GHz Core2 CPU (Download source code).
2010.05.12 comments -