deepcopy()非常慢 [英] deepcopy() is extremely slow
问题描述
我在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.
编辑:通过明智地实施__deepcopy__
在事物上将其降低到8%.尽管如此,还不够好. (足够好于1%或更少,我打算为此扔很多东西.)timeit
表示每个deepcopy()
41.8ms.
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()
.
推荐答案
实际上,深度复制非常慢.但是我们可以使用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快,但是泡菜...
as what we can see, json/ujson/cPickle is faster than deepcopy, but pickle...
这篇关于deepcopy()非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!