Python:使用`pickle`或`marshal`和使用`re`的性能比较 [英] Python: performance comparison of using `pickle` or `marshal` and using `re`

查看:92
本文介绍了Python:使用`pickle`或`marshal`和使用`re`的性能比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python计算一些非常大的数字,我想将先前计算的结果存储在Berkeley DB中。

I am calculating some very large numbers using Python, and I'd like to store previously calculated results in Berkeley DB.

问题是Berkeley DB必须使用字符串,并且我必须为计算结果存储一个整数元组。

The problem is that Berkeley DB has to use strings, and I have to store an integer tuple for the calculation results.

例如,我得到(m,n)作为我的结果,一种方法是将其存储为%d,%d%(m,n)并使用<$ c $读出c> re 。我还可以使用 pickle marshal 存储元组。

For example, I get (m, n) as my result, one way is to store this as "%d,%d" % (m, n) and read it out using re. I can also store the tuple using pickle or marshal.

哪个具有更好的性能?

推荐答案

对于纯速度,元帅将为您提供最快的结果。

For pure speed, marshal will get you the fastest results.

时间:

>>> timeit.timeit("pickle.dumps([1,2,3])","import pickle",number=10000)
0.2939901351928711
>>> timeit.timeit("json.dumps([1,2,3])","import json",number=10000)
0.09756112098693848
>>> timeit.timeit("pickle.dumps([1,2,3])","import cPickle as pickle",number=10000)
0.031056880950927734
>>> timeit.timeit("marshal.dumps([1,2,3])","import marshal", number=10000)
0.00703883171081543

这篇关于Python:使用`pickle`或`marshal`和使用`re`的性能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆