[英]Optimizing Vanilla Python with Pandas and Numpy
Is there anyway that I could make the function below faster and more optimized with pandas
or numpy
, the function below adds the sum of seq45
until the elements of it is equivalent or over 10000.The elements that is being added up to seq45
are 3,7,11
的順序。 我想提高速度的原因是必須可能用更大的數值(如 1000000)測試 10000 並更快地處理。 此代碼的答案來自此問題: 問題
代碼:
Sequence = np.array([3, 7, 11])
seq45= []
for n in itertools.cycle(Sequence):
seq.append(n)
if sum(seq45) >= 10000: break
print(seq45)
當前性能/處理時間:71.9ms
你可以試試:
Sequence = np.array([3, 7, 11])
s = Sequence.sum()
tot = 10000
seq = list(Sequence)*(tot//s)
mod = tot%s
for n in seq:
if mod > 0:
seq.append(n)
mod -= n
else:
break
掛壁時間:57 µs
import numpy as np
tot = 10_000
sequence = np.array([3, 7, 11])
n = tot // sequence.sum() + 1
seq45 = np.tile(sequence, n)
請注意,這並不完全等效,因為它重復了完整的序列,而不是單個元素,因此seq45
可能會稍大一些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.