问题描述
我遇到了有关字典的内存管理,其中提到了 intern 功能。
I came across this question about memory management of dictionaries, which mentions the intern function. What exactly does it do, and when would it be used?
举个例子:(如果我有一个名为 seen 的集合) ,其中包含(string1,string2)形式的元组(我用来检查重复项)会存储(intern(string1),intern(string2))提高性能内存还是速度?
To give an example: if I have a set called seen, that contains tuples in the form (string1,string2), which I use to check for duplicates, would storing (intern(string1),intern(string2)) improve performance w.r.t. memory or speed?
推荐答案
来自文档
sys.intern(string)
内联的弦线不朽;您必须保留对intern()的
返回值的引用,以从中受益。
Interned strings are not immortal; you must keep a reference to the return value of intern() around to benefit from it.
:
正如文档所建议的那样, sys.intern
函数旨在用于性能优化。
As the documentation suggests, the sys.intern
function is intended to be used for performance optimization.
sys.intern
函数维护一个 interned表字符串。当您尝试内联一个字符串时,该函数会在表中查找它,并且:
The sys.intern
function maintains a table of interned strings. When you attempt to intern a string, the function looks it up in the table and:
-
如果该字符串不存在(还没有被内联)该函数将
保存到表中并从内联字符串表中返回。
If the string does not exists (hasn't been interned yet) the function savesit in the table and returns it from the interned strings table.
>>> import sys
>>> a = sys.intern('why do pangolins dream of quiche')
>>> a
'why do pangolins dream of quiche'
在上面的示例中, a
保留实习字符串。即使不可见, sys.intern
函数也保存了为什么穿山甲梦见乳蛋饼
字符串
In the above example, a
holds the interned string. Even though it is not visible, the sys.intern
function has saved the 'why do pangolins dream of quiche'
string object in the interned strings table.
如果该字符串存在(已被嵌入),则该函数从
嵌入字符串表中返回它。
If the string exists (has been interned) the function returns it from theinterned strings table.
>>> b = sys.intern('why do pangolins dream of quiche')
>>> b
'why do pangolins dream of quiche'
即使不是立即可见,因为字符串'为什么穿山甲梦到乳蛋饼'
已经被检查过,所以 b
现在拥有与 a
。
Even though it is not immediately visible, because the string 'why do pangolins dream of quiche'
has been interned before, b
holds now the same string object as a
.
>>> b is a
True
如果我们创建相同的字符串而不使用实习生,那么最终
If we create the same string without using intern, we end up with two different string objects that have the same value.
>>> c = 'why do pangolins dream of quiche'
>>> c is a
False
>>> c is b
False
使用 sys.intern
可以确保您永远不会创建两个具有相同值的字符串对象-当您请求创建另一个与现有字符串具有相同值的字符串对象时对象,您将收到对现有字符串对象的引用。这样,您节省内存。而且,字符串对象比较现在非常有效,因为它是通过比较两个字符串对象的内存地址而不是它们的内容来进行的。
By using sys.intern
you ensure that you never create two string objects that have the same value—when you request the creation of a second string object with the same value as an existing string object, you receive a reference to the pre-existing string object. This way, you are saving memory. Also, string objects comparison is now very efficient because it is carried out by comparing the memory addresses of the two string objects instead of their content.
这篇关于sys.intern()的作用是什么?何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!