避免由于性能而进行深度复制 [英] Avoid deepcopy due to performance

查看:72
本文介绍了避免由于性能而进行深度复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中还有3个列表。我需要对列表进行一些操作,因为我有180.000的深度复制步骤已经花费了2.5s来复制一次列表。
包括操作在内的总时间花费了220s计算时间中的80s。

I have a list with another 3 lists in it. I need to do some operations on the lists, as i have like 180.000 of those the step of deepcopying already takes 2,5s to copy the lists once. The overall time including operations takes 80s out of 220s compution time.

s = [[1,2,3],
     [4,5,6],
     [7,8,9]]
s1 = copy.deepcopy(s)
s1[0][0] = s[1][1]
s1[1][1] = s[0][0]

所示的操作需要重复一百万次。因此,深度复制使我面临性能瓶颈。

The shown operation needs to be repeated like a million of times. So deepcopy makes me face a performance bottleneck.

是否有更高效的方法或另一种方法来取消引用列表 s

Is there a more performant way or a different approach to "unreference" the list s?

推荐答案

deepcopy 似乎有一些开销检查所有能够处理的不同情况。如果您的列表始终是列表列表(一层嵌套),则可以尝试仅使用 s = [x中的x的列表(x)] s = list(map(list,s))。两者似乎都快得多:

deepcopy seems to have some overhead for checking all those different cases it is able to handle. If your lists are always lists of lists (one level of nesting), then you can try to use just s = [list(x) for x in s] or s = list(map(list, s)). Both seem to be quite a bit faster:

In [9]: s = [[random.randint(1, 1000) for _ in range(100)] for _ in range(100)]
In [10]: %timeit copy.deepcopy(s)
10 loops, best of 3: 22.7 ms per loop
In [11]: %timeit [list(x) for x in s]
10000 loops, best of 3: 123 µs per loop
In [18]: %timeit list(map(list, s))
10000 loops, best of 3: 111 µs per loop






或者,根据您的应用程序,最好不要复制并存储(已修改的)列表本身,而只是以修改后的单元格形式或作为命令来进行修改堆。


Alternatively, depending on your application, it might be better not to copy and store the (modified) lists themselves, but just the modification, either in the form of the modified cells, or as a command stack.

这篇关于避免由于性能而进行深度复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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