前言

在 Python 中经常通过内存池化技术来提高其性能,那么问题来了,在什么情况下会池化呢? 让我们通过几个例子进行一下理解一下.

预备知识

在查看例子之前,首先要提 python 中的一个函数 id(),让我们看一下函数说明:

id(obj, /)
Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object \'s memory address.)

通过上述说明,可以得知 id() 将会返回对像的唯一标识,在 CPython 中将会返回内存地址,也就是说如果两个对象 的 id 值一样,可以说着两个对象是相同的了.

例子

example 00

a = ""
b = ""
print(id(a),id(b))
print(a is b)

输出结果:

>>> a = ""
>>> b = ""
>>> print(id(a),id(b))
2114853370544 2114853370544
>>> print(a is b)
True

example 01

a = "a"
b = "a"
print(id(a),id(b))
print(a is b)

输出结果:

>>> a = "a"
>>> b = "a"
>>> print(id(a),id(b))
2114883022608 2114883022608
>>> print(a is b)
True

example 02

a = "magic_string"
b = "magic" + "_" + "string"
print(id(a),id(b))
print(a is b)

输出结果:

>>> a = "magic_string"
>>> b = "magic" + "_" + "string"
>>> print(id(a),id(b))
2114887161136 2114887161136
>>> print(a is b)
True

example 03

a = "magic!"
b = "mgaic!"
print(id(a),id(b))
print(a is b)

输出结果:

>>> a = "magic!"
>>> b = "mgaic!"
>>> print(id(a),id(b))
2114885855416 2114889455408
>>> print(a is b)
False

example 04

a,b = "magic!","magic!"
print(id(a),id(b))
print(a is b)

输出结果:

>>> a,b = "magic!","magic!"
>>> print(id(a),id(b))
2114885691912 2114885691912
>>> print(a is b)
True

example 05

a = "!"
b = "!"
print(id(a),id(b))
print(a is b)

输出结果:

>>> a = "!"
>>> b = "!"
>>> print(id(a),id(b))
140564571922024 140564571922024
>>> print(a is b)
True

example 06

print(a*20 is 'aaaaaaaaaaaaaaaaaaaa')
print(a*21 is 'aaaaaaaaaaaaaaaaaaaaa')

输出结果:

>>> print(a*20 is 'aaaaaaaaaaaaaaaaaaaa')
False
>>> print(a*21 is 'aaaaaaaaaaaaaaaaaaaaa')
False

总结

通过上述 7 个例子,我们不难对 python 的字符串池化有个大概的认识,我们这里做个简单的总结:

  1. 通过 example 00,01,05,我们可以得出对于长度为 0 或者 1 的字符串会被池化
  2. 通过 example 02,03,我们可以得出字符串中只包含字母数字以及下划线的字符串会被池化
  3. 通过 example 04, 我们可以得出当在同一行对不同变量,赋值如果相同的话,它们将会指向同一个对象,注意这里面的 “magic!” 并不符合池化的要求,这只是一种编译器的优化
  4. example 06 所出现的现象在 python 中有一个专业的术语,讲常量折叠(constant folding),顾名思义,在编译优化时,讲能够计算出的结果的变量直接替换为常量.但是这没有限制吗?显然不是的,在我们的例子中已经发现,当长度超过20的时候,折叠就会失效了,试想一下,如果没有限制的话,初始化的字符串过长,将会严重导致性能的下降以及内存的消耗

参考链接

  1. The internals of Python string interning

  2. exploring python code objects

  3. Python string interning

  4. Python String objects implementation

最新文章

  1. Python列表切成多个
  2. 如何系统地自学一门Python 语言(转)
  3. js 内存小记
  4. sys.path和os.path
  5. 简单的猜数字(JAVA版)
  6. C#中的逆变和协变
  7. JAVA printWriter中write()和println()区别
  8. UIAlertController基本使用与内存泄露分析!!!
  9. Vue 爬坑之路(三)—— 使用 vue-router 跳转页面
  10. HEOI2016 题解
  11. 1-自己动手编写ArrayList集合
  12. Servlet基本
  13. C语言实现大数四则运算
  14. HTML+Javascript制作拼图小游戏详解(终)
  15. 数据序列化导读(1)[JSON]
  16. 补交课下测试(ch12并发编程) 08.第八周
  17. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)
  18. Stream(流)的基本操作
  19. git 错误 fatal: loose object...is corrupt
  20. OCP新题库,052新加的考题及答案整理-24题

热门文章

  1. THINKPHP导入全部post参数
  2. 如何在 block 中修改外部变量
  3. Linux下查看Raid磁盘阵列信息的方法
  4. mysqldump 备份单个数据库
  5. Oracle某些功能实现语句处理方法
  6. nginx检查报错 error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory
  7. rpc简介、原理、实例
  8. 超烂的ELK之filebeat读取【已解决】
  9. LeetCode: Surrounded Regions 解题报告
  10. LeetCode: Longest Consecutive Sequence 解题报告