簡體   English   中英

使用 Pandas 和 Numpy 優化 Vanilla Python

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM