python deepcopy的替代方案
Python 方案 替代
2023-09-14 09:06:35 时间
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
有点问题,如果字典的值为对象那么会有问题。
相关文章
- 二级Python选择题_二级python选择题题库
- 一个 15 年未修补 Python 漏洞让攻击者可以执行代码:35 万个开源代码存储库岌岌可危
- python字符串编码及乱码解决方案
- Python基本数据类型-Number
- python监控网站更新_Python 通过网站search功能监控网站内容更新[通俗易懂]
- 安利一款Python开发的仿Linux树形显示目录tree命令「建议收藏」
- python生成器详解_Python 生成器
- pycharm如何导入自定义模块_Python添加模块
- 遗传算法的应用实例python实现_遗传算法Python解决一个问题
- python输出unicode编码_Python以utf8编码读取文件
- python的特点和优势_Java与Python异同
- python类的继承
- Python 中最快的循环方式
- 在python中用来安装第三方库的常用工具_什么库用于安装管理Python扩展包
- python里面的缩进是什么意思_Python缩进规则(一看即懂)[通俗易懂]
- python hexdump_笨办法学 Python · 续 练习 26:`hexdump`
- python中if判断语句的用法_Python if判断语句的用法详细介绍[通俗易懂]
- VSCode中设置Python语言自动格式化的方案
- Python选择结构IF语句_Python自学第五节
- Python面向对象编程-魔术方法-__call__和__getattr__方法
- 基于MongoDB的python日志功能详解大数据
- Python 缓存服务详解编程语言
- Linux下如何快速卸载Python环境(linux如何卸载python)
- Linux环境下安装Python(linux装python)
- Python操作Redis实现数据持久化(python操作redis)
- Python如何连接PostgreSQL数据库?(python连接postgresql)
- Linux Python:开启互联网新时代(linux python)