deepcopy() 非常慢 [英] deepcopy() is extremely slow

查看:73
本文介绍了deepcopy() 非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中有一个包含大约 1000 个对象(行星系统 + 恒星 + 行星)的游戏状态,我需要复制它并在需要时对其应用一系列转换.然而,在大约 1 个请求/秒时,这 占用了我 24.63% 的运行时间.我怎样才能让它快点?请注意,复制较少不是一种选择,因为转换几乎涉及所有内容.

I have a game state in Python with about 1000 objects (planetary systems + stars + planets), and I need to copy it and apply a bunch of transformations to it when requested. However, at about 1 request/second, this is taking up 24.63% of my runtime. How can I make it go fast? Note that copying less is not an option, since the transforms touch just about everything.

EDIT:通过在事物上明智地实施 __deepcopy__,将其降低到 8%.尽管如此,还是不​​够好.(足够好是 1% 或更少,我计划在这方面投入更多的东西.)timeit 说每个 deepcopy() 需要 41.8 毫秒.

EDIT: got it down to 8% with judicious implementation of __deepcopy__ on things. Still, not good enough. (Good enough is 1% or less, I plan on throwing many more things at this.) timeit says 41.8ms per deepcopy().

推荐答案

其实deepcopy是很慢的.但是我们可以使用 json、ujson 或 cPickle.我们可以使用 json/cPickle 来转储一个对象,然后再加载它.这是我的测试:

Actually, deepcopy is very slow. But we can use json, ujson, or cPickle. we can use json/cPickle to dump an object, and load it later. This is my test:

Total time: 3.46068 s
File: test_deepcopy.py
Function: test at line 15
Line #   Hits          Time Per Hit   % Time  Line Contents
==============================================================
15                                             @profile
16                                             def test():
17       100       957585   9575.9     27.7        b = deepcopy(a)
18       100          862      8.6      0.0        c = copy(a)
19       100        42295    422.9      1.2        d = ujson.loads(ujson.dumps(a))
20       100        85040    850.4      2.5        e = json.loads(json.dumps(a))
21       100      2323465  23234.7     67.1        f = pickle.loads(pickle.dumps(a, -1))
22       100        51434    514.3      1.5        g = cPickle.loads(cPickle.dumps(a, -1))

正如我们所见,json/ujson/cPickle 比 deepcopy 快,但是 pickle...

as what we can see, json/ujson/cPickle is faster than deepcopy, but pickle...

这篇关于deepcopy() 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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