python - Python:MemoryError,我的代码内存效率不高
问题描述
我有一个编码测验: [a,b,c,d,e,f] 其中 a != b != c != d != e != f
找出 1-6 中所有可能的数字,其中 (b+c)-a = (d+e+f)-(b+c)。
from itertools import permutations
import numpy
perm = permutations([1, 2, 3, 4, 5, 6])
for i in list(perm):
my_array = numpy.asarray(i)
if (my_array[1] + my_array[2]) - my_array[0] == (my_array[3] + my_array[4] + my_array[5]) - (my_array[1] + my_array[2]):
print(my_array)
else:
pass
该代码工作正常,但内存效率不高。有人可以帮我吗?
解决方案
首先,尽可能避免循环是一个好习惯:https ://en.wikipedia.org/wiki/Loop_optimization
此外,很好地查看底层数学,看看它是否可以导致进一步优化:我不是数学家,所以可能还有更多空间,但我们已经可以这样说:
b+c-a=d+e+f-(b+c)
(b+c)-a - ((d+e+f)-(b+c))=0 # moving everything to the left
-a+2b+2c-d-e-f=0 # consolidating terms
所以现在它是一个简单的术语总和,我们可以更有效地使用 Numpy 进行处理:
from itertools import permutations
import time
import numpy as np
low = 1
high = 6
perms = np.array(list(permutations(range(low, high + 1))))
print('permutations cnt:', perms.shape[0])
time_np = 0
time_loop = 0
runs = 1000
for run in range(0, runs):
# ---------------------------------------------
# NUMPY
# ---------------------------------------------
start = time.process_time()
equation = np.zeros((perms.shape[0], 6))
equation[:, 0] = -perms[:, 0] # -a
equation[:, 1] = 2 * perms[:, 1] # +2b
equation[:, 2] = 2 * perms[:, 2] # +2c
equation[:, 3] = -perms[:, 3] # -d
equation[:, 4] = -perms[:, 4] # -e
equation[:, 5] = -perms[:, 5] # -f
sum = np.sum(equation, axis=1) # summing up rows
matches_np = sum == 0 # = 0
end = time.process_time()
time_np += end - start
# ---------------------------------------------
# LOOP
# ---------------------------------------------
start = time.process_time()
matches_loop = []
for i in perms:
(a, b, c, d, e, f) = i
if b + c - a == (d + e + f - (b + c)):
matches_loop.append(i)
end = time.process_time()
time_loop += end - start
# ---------------------------------------------
# COMPARING
# ---------------------------------------------
time_np_avg = time_np / runs
time_loop_avg = time_loop / runs
print('\nNumpy method:')
print(' matches cnt:', matches_np.sum())
print(' avg time spent (sec):', '%f' % time_np_avg)
print('\nLoop method:')
print(' matches cnt:', len(matches_loop))
print(' avg time spent (sec):', '%f' % time_loop_avg)
print('\nNumpy is', time_loop_avg / time_np_avg, ' faster than loop')
结果:
因此,即使对于像这样的低计算任务,它仍然可以通过循环执行,我强烈建议您查看数学方面并开始使用 Numpy 之类的库(以及稍后使用 CUDA 等加速)编写代码来构建编写高效代码的习惯。
推荐阅读
- r - strsplit 返回带有反斜杠和引号的嵌套列表 \"
- python - 如何从列表中随机选择一个元素,然后使用一个简单的公式来更改这个值(并在 for 循环中进行)?
- quarkus - oidc-client:grant.type=password 不支持其他授权选项
- database - 运输区 - 表费率 - 不保存信息
- vue.js - 如何使用 wepack devserver 代理静态文件?
- python - TypeError:“类”对象不可迭代-flask/sqlalchemy/jinja
- python - 在烧瓶应用程序中使用 sqlalchemy 更新 postgresql 记录
- python - python:测试文本字段中是否存在多个字符串以设置新列值
- .net-core - contentrootpath 指向 C:/Program Files/IISExpress
- reactjs - Focus Trap React 和 2 个弹出窗口中的一些组件