首页 > 解决方案 > 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

该代码工作正常,但内存效率不高。有人可以帮我吗?

标签: pythonarraysout-of-memory

解决方案


首先,尽可能避免循环是一个好习惯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 等加速)编写代码来构建编写高效代码的习惯。


推荐阅读