python sys.intern是做什么的,什么时候应该使用它? [英] What does python sys.intern do, and when should it be used?

查看:89
本文介绍了python sys.intern是做什么的,什么时候应该使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了此问题有关字典的内存管理,其中提到了 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) )提高性能内存还是速度?

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?

推荐答案

来自

在"interned"字符串表中输入字符串,然后返回 内联字符串–字符串本身或副本.实习字符串 有助于提高字典查找的性能-如果 字典中的键被实习,查找键被实习, 键比较(散列后)可以通过指针比较来完成 而不是字符串比较.通常,Python中使用的名称 程序会自动被冻结,而字典则用于保留 模块,类或实例属性具有互连键.

Enter string in the table of "interned" strings and return the interned string – which is string itself or a copy. Interning strings is useful to gain a little performance on dictionary lookup – if the keys in a dictionary are interned, and the lookup key is interned, the key comparisons (after hashing) can be done by a pointer compare instead of a string compare. Normally, the names used in Python programs are automatically interned, and the dictionaries used to hold module, class or instance attributes have interned keys.

内嵌的字符串不是不朽的;您必须保留对 可以从中受益的返回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:

  1. 如果字符串不存在(尚未被实习),函数将保存 在表格中将其返回,然后从插入的字符串表中将其返回.

  1. If the string does not exists (hasn't been interned yet) the function saves it 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函数也已将'why do pangolins dream of quiche'字符串对象保存在插入的字符串表中.

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 the interned strings table.

>>> b = sys.intern('why do pangolins dream of quiche')
>>> b
'why do pangolins dream of quiche'

即使它不是立即可见的,因为字符串'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

如果我们在不使用intern的情况下创建相同的字符串,则最终将得到两个具有相同值的不同字符串对象.

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.

这篇关于python sys.intern是做什么的,什么时候应该使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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