给定一个大数组。我正在寻找所有元素相加小于limit
的索引。我找到了两种方法:
import time as tm
import numpy as nm
# Data that we are working with
large = nm.array([3] * 8000)
limit = 23996
# Numpy version, hoping it would be faster
start = tm.time() # Start timing
left1 = nm.tril([large] * len(large)) # Build triangular matrix
left2 = nm.sum(left1, 1) # Sum up all rows of the matrix
idx = nm.where(left2 >= limit)[0][0] # Check what row exceeds the limit
stop = tm.time()
print "Numpy result :", idx
print "Numpy took :", stop - start, " seconds"
# Python loop
sm = 0 # dynamic sum of elements
start = tm.time()
for i in range(len(large)):
sm += large[i] # sum up elements one by one
if sm >= limit: # check if the sum exceeds the limit
idx = i
break # If limit is reached, stop looping.
else:
idx = i
stop = tm.time()
print "Loop result :", idx
print "Loop took :", stop - start, " seconds"
很不幸,如果数组非常大,numpy版本会耗尽内存。当我说更大时,我的意思是100,000个值。当然,这会得到一个大矩阵,但是for循环同样需要2分钟才能遍历完这100,000个值。那么瓶颈在哪里?如何加速此代码?
cumsum()
是最大的区别。 - lyvic