作家
登录

从环境设置到内存分析:Python代码优化指南

作者: 来源: 2017-07-18 15:00:58 阅读 我要评论

PyPy 的一个缺点是因为其 JIT 和垃圾一样的收受接收站实现,它平日会应用比 CPython 更多的内存。然则在某些案例中,其的内存消费会比 CPython 少。下面我们来看看你可以若何测量你的应用应用了若干内存。

诊断内存应用

pip install memory_profiler

别的还要安装 psutil 依附包:

pip install psutil

这个对象的长处是它会在一个 Python 脚本一一行行地显示内存消费。这可以让我们找到脚本中可以被我们重写的地位。但这种分析有一个缺点。你的代码的运行速度比一般脚本慢 10 到 20 倍。

怎么应用它?你只须要在你须要测量的函数上直接加上 @profile() 即可。让我们看看实际怎么操作!我们将应用之前用过的素材脚本作为模型,但做了一点修改,移除了统计部分。代码也可在 GitHub 查看:https://github.com/apatrascu/hunting-python-performance/blob/master/02.primes-v1.py


from memory_profiler import profile
Supported: yes


def primes(n):
if n == 2:
elif n < 2:
return []
s = range(3, n + 1, 2)
mroot = n ** 0.5
half = (n + 1) / 2 - 1
i = 0
m = 3
s = range(3, n + 1, 2)
while m <= mroot:
if s[i]:

硬件功能


j = (m * m - 3) / 2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i + 1
m = 2 * i + 3
return [2] + [x for x in s if x]

15 57.683594 MiB 22.183594 MiB while m <= mroot:

42 58.019531 MiB 0.000000 MiB while j < half:
len(primes(100000))
pypy -m memory_profiler 02.primes-v3.py

或者直接裹足本中导入 memory_profiler:

pypy -m memory_profiler 02.primes-v3.py

在履行完这行代码之后,我们可以看到 PyPy 获得如许的结不雅:

Line #    Mem usage    Increment   Line Contents
================================================
54 35.312500 MiB 0.000000 MiB @profile(precision=6)
55 def primes(n):
56 35.351562 MiB 0.039062 MiB if n == 2:
57 return [2]
58 35.355469 MiB 0.003906 MiB elif n < 2:
59 return []
36 35.417969 MiB 0.000000 MiB i = 0
60 35.355469 MiB 0.000000 MiB s = []
mroot = n ** 0.5
61 59.515625 MiB 24.160156 MiB for i in range(3, n+1):
62 59.515625 MiB 0.000000 MiB if i % 2 != 0:
63 59.515625 MiB 0.000000 MiB s.append(i)
64 59.546875 MiB 0.031250 MiB mroot = n ** 0.5
65 59.550781 MiB 0.003906 MiB half = (n + 1) / 2 - 1
45 58.019531 MiB 0.000000 MiB i = i + 1

66 59.550781 MiB 0.000000 MiB i = 0
67 59.550781 MiB 0.000000 MiB m = 3
68 59.554688 MiB 0.003906 MiB while m <= mroot:
69 59.554688 MiB 0.000000 MiB if s[i]:
70 59.554688 MiB 0.000000 MiB j = (m * m - 3) / 2
71 59.554688 MiB 0.000000 MiB s[j] = 0

如不雅你没有 cpupower,可应用以下敕令安装:


73 59.554688 MiB 0.000000 MiB s[j] = 0
75 59.554688 MiB 0.000000 MiB i = i + 1
76 59.554688 MiB 0.000000 MiB m = 2 * i + 3
77 59.554688 MiB 0.000000 MiB l = [2]
78 59.679688 MiB 0.125000 MiB for x in s:
79 59.679688 MiB 0.000000 MiB if x:
80 59.679688 MiB 0.000000 MiB l.append(x)
81 59.683594 MiB 0.003906 MiB return l

我们可以看到这个脚本应用了 24.371094 MiB 的 RAM。让我们简单分析一下。我们看到个中大年夜多半都用在了数值数组的构建中。它清除了偶数数值,保存了所有其它数值。

我们可以经由过程调用 range 函数而对其进行一点改进,其应用一个增量参数。在这个案例中,该脚本看起来像是如许:

from memory_profiler import profile


elif n < 2:
def primes(n):
12 35.500000 MiB 0.000000 MiB half = (n + 1) / 2 - 1
if n == 2:
return [2]
return []
mroot = n ** 0.5
half = (n + 1) / 2 - 1
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m * m - 3) / 2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i + 1
m = 2 * i + 3
l = [2]
for x in s:
if x:
l.append(x)
return l

@profile(precision=6)

  推荐阅读

  十大经典排序算法的JS版

【技巧沙龙】AI开辟者拭魅战营-7分钟打造1个定制技能。7月22号,我们等你一路! 读者自行测验测验可以想看源码戳这(https://github.com/damonare/Sorts),博主在github建了个库,读者可以>>>详细阅读


本文标题:从环境设置到内存分析:Python代码优化指南

地址:http://www.17bianji.com/lsqh/36268.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)