如何Python的垃圾收集与numpy的阵列追加和删除? [英] How does Pythonic garbage collection with numpy array appends and deletes?

查看:123
本文介绍了如何Python的垃圾收集与numpy的阵列追加和删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图适应密谋code(matplotlib),它更新一个定时器使用Python列表的情节数据使用numpy的阵列去的底层结构。我希望能够以较低的情节,尽可能的时间步长,而且由于数据可能会向上进入千点,我开始失去了宝贵的时间快,如果我不能。我知道numpy的阵列pferred对于这样的事情$ P $,但我有麻烦找出当我需要这样想一个Python程序员,当我需要这样想一个C ++程序员最大限度地发挥我的内存访问效率。

I am trying to adapt the underlying structure of plotting code (matplotlib) that is updated on a timer to go from using Python lists for the plot data to using numpy arrays. I want to be able to lower the time step for the plot as much as possible, and since the data may get up into the thousands of points, I start to lose valuable time fast if I can't. I know that numpy arrays are preferred for this sort of thing, but I am having trouble figuring out when I need to think like a Python programmer and when I need to think like a C++ programmer maximize my efficiency of memory access.

它说,在scipy.org文档为它返回附加在一起阵列的副本追加()函数。做所有这些副本得到适当的垃圾回收?例如:

It says in the scipy.org docs for the append() function that it returns a copy of the arrays appended together. Do all these copies get garbage-collected properly? For example:

import numpy as np

a = np.arange(10)
a = np.append(a,10)
print a

这是我是怎么回事,在C ++的阅读 - 的水平,但如果我知道我在说什么,我就不会问这个问题,所以请纠正我,如果我错了! = P

This is my reading of what is going on on the C++-level, but if I knew what I was talking about, I wouldn't be asking the question, so please correct me if I'm wrong! =P

初​​的10个整数的块被分配,并且该符号一个指向该块的开头。然后的11位的整数一个新块被分配,对正在使用的一共有21个整数(84字节)。然后将一个指针被移动到11-INT块的开始。我的猜测是,这将导致垃圾收集算法递减10-INT块的引用计数为零,取消分配它。这是正确的吗?如果没有,我如何确保追加的时候我没有创建开销?

First a block of 10 integers gets allocated, and the symbol a points to the beginning of that block. Then a new block of 11 integers is allocated, for a total of 21 ints (84 bytes) being used. Then the a pointer is moved to the start of the 11-int block. My guess is that this would result in the garbage-collection algorithm decrementing the reference count of the 10-int block to zero and de-allocating it. Is this right? If not, how do I ensure I don't create overhead when appending?

我也是不知道如何正确地删除numpy的阵列时,我使用它完成。我有我的情节一个复位按钮,只是刷新了所有的数据,然后重新开始。当我有名单,这是用做删除数据[:] 。是否有numpy的阵列等效功能?或者我应该只是说数据= np.array([]),并依靠垃圾收集器做的工作适合我?

I also am not sure how to properly delete a numpy array when I am done using it. I have a reset button on my plots that just flushes out all the data and starts over. When I had lists, this was done using del data[:]. Is there an equivalent function for numpy arrays? Or should I just say data = np.array([]) and count on the garbage collector to do the work for me?

推荐答案

自动内存管理的一点是,你不会去想它。在code你写的副本将垃圾收集罚款(这是在附近上不可能混淆Python的内存管理)。但是,由于 np.append 不-到位,code将在内存中创建一个新的数组(含 A串联 10 ),然后变量 A 将更新为指向这个新的数组。由于 A 现在不再指向原始数组,其中有1引用计数,它的引用计数减为0,它会自动清理。您可以使用 GC.Collect的 来强制完整的清理工作。

The point of automatic memory management is that you don't think about it. In the code that you wrote, the copies will be garbage-collected fine (it's nigh on impossible to confuse Python's memory management). However, because np.append is not in-place, the code will create a new array in memory (containing the concatenation of a and 10) and then the variable a will be updated to point to this new array. Since a now no longer points to the original array, which had a refcount of 1, its refcount is decremented to 0 and it will be cleaned up automatically. You can use gc.collect to force a full cleanup.

Python的力量不在于微调内存访问,虽然它可以优化。你可能最排序pre-分配 A (例如使用 A = np.zeros(小于大小>));如果你需要更精细的调整以外,它开始变得有点毛。你可以看看在用Cython + numpy的教程获取到C与Python集成的效率非常整洁和简单的方法。

Python's strength does not lie in fine-tuning memory access, although it is possible to optimise. You are probably best sorted pre-allocating a (using e.g. a = np.zeros( <size> )); if you need finer tuning than that it starts to get a bit hairy. You could have a look at the Cython + Numpy tutorial for a very neat and easy way to integrate C with Python for efficiency.

在Python变量只是点到其内容的存储位置;您可以删除任何变量,它会减少一个目标的引用计数。目标会自动的引用计数降为零后进行清洗。这样做的核心是,不用担心清理你的记忆。它会自动发生。

Variables in Python just point to the location where their contents are stored; you can del any variable and it will decrease the reference count of its target by one. The target will be cleaned automatically after its reference count hits zero. The moral of this is, don't worry about cleaning up your memory. It will happen automatically.

这篇关于如何Python的垃圾收集与numpy的阵列追加和删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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