python deepcopy的替代方案
作者:互联网
from copy import deepcopy
import marshal
import timeit
from multidict import CIMultiDict
def test_deepcopy():
_kwargs = CIMultiDict({
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Host": "www.discogs.com",
"User-Agent": ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"),
})
if _kwargs is None:
_kwargs = dict()
kwargs = deepcopy(_kwargs)
def test_marshal():
_kwargs = CIMultiDict({
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Host": "www.discogs.com",
"User-Agent": ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"),
})
if _kwargs is None:
_kwargs = dict()
kwargs = marshal.loads(marshal.dumps(dict(_kwargs)))
if __name__ == '__main__':
print(timeit.timeit(stmt=test_deepcopy, number=1000000))
print("=" * 30)
print(timeit.timeit(stmt=test_marshal, number=1000000))
测试发现deepcopy是比较耗时的
第一次
21.046875760000002
==============================
6.435647065000001
第二次
19.536858445
==============================
6.0979895100000014
标签:0.9,timeit,python,Accept,kwargs,deepcopy,替代,marshal 来源: https://www.cnblogs.com/c-x-a/p/12374692.html