寻找有关Python内存分配的信息

寻找有关Python内存分配的信息

本文介绍了寻找有关Python内存分配的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,有人能帮帮我吗?我已经花了一个没有结果的

小时谷歌搜索没有运气。


我正在讨论内存分配技术

有人,我正试图找到一个引用 - 我想/ b $ b想想 - 蒂姆·彼得斯在哪里讨论Python的方式

在你附加到列表时分配内存。在基本的

条款中,他说每当你试图附加到已经满的

列表时,Python的大小就会增加一倍。

列表。这浪费不超过该列表所需的内存的50%,但有各种优势 - 而且如果我能记住那些是什么,我该死的

的优点是。


有人能指出我正确的方向吗?


谢谢,

-

史蒂文。

解决方案








分配额外的内存通常需要以下步骤:


- 分配更多内存

- 复制从旧内存到新内存的所有数据

- 释放旧内存


如果
$中有N个数据项,则第二步需要N个操作b $ b列表。如果你为每个项目添加一个''block''内存到你添加的每个项目,你就不会浪费任何内存,但是每个添加的项目都需要N个步骤来

copy所有旧物品。这意味着在你添加了N个项目之后,

N **已经采取了两个步骤。这对性能来说非常糟糕。


另一方面,如果你每次用完时双倍内存,

你必须复制更少的数据,最后你需要

大约N步骤将N个项目添加到列表中。那好多了,不是吗?b / b $

Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa


Can somebody help me please? I''ve spent a fruitless
hour googling with no luck.

I''m discussing memory allocation techniques with
somebody, and I''m trying to find a quote from -- I
think -- Tim Peters where he discusses the way Python
allocates memory when you append to lists. In basic
terms, he says that every time you try to append to a
list that is already full, Python doubles the size of
the list. This wastes no more than 50% of the memory
needed for that list, but has various advantages -- and
I''m damned if I can remember exactly what those
advantages were.

Can anyone point me in the right direction?

Thanks,
--
Steven.

解决方案







Allocating extra memory usually requires the following steps:

- Allocate more memory
- Copy all data from old to new memory
- Deallocate old memory

That second step requires N actions if there are N data items in the
list. If you add one ''block'' of memory to the list for each item you
add, you waste no memory, but every item added requires N steps to
copy all the old items. That means that after you''ve added N items,
N**2 steps have been taken. That''s very bad for performance.

If, on the other hand, you double the memory every time you run out,
you have to copy much less data, and in the end it turns out you need
roughly N steps to add N items to the list. That''s a lot better, isn''t
it?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


这篇关于寻找有关Python内存分配的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:57