0

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 in order. The reason why i want to increase the speed is to have have to possibly test 10000 with larger number values like 1000000 and to be processed faster. The answer to this code was gotten from this issue: issue

Code:

Sequence = np.array([3, 7, 11])
seq45= []
for n in itertools.cycle(Sequence):
    seq.append(n)
    if sum(seq45) >= 10000: break
        
print(seq45)

Current Perfromance/ Processing time: 71.9ms

enter image description here

1
  • The intelligent answers are the ones below, which take advantage of the mathematics of the situation. You don't really have to go through 100,000 iterations. You know that you will have 100000//(3+7+11) complete sets, so you only have to check the leftovers one by one. That will work instantly even with 1,000,000,000 iterations. Commented Mar 6, 2021 at 6:45

2 Answers 2

1

You can try:

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

Wall time: 57 µs

Sign up to request clarification or add additional context in comments.

1 Comment

Fun fact: testing this and my own answer (which are, as pointed out, not precisely equivalent), shows this answer to be slightly faster than using np.tile. But only for tot up to about a million. For larger tot values, np.tile starts to win (my guess is that list(Sequence) * (tot//s) will be expensive), but still only by a small margin. For some things, Python is well optimised.
1
import numpy as np

tot = 10_000
sequence = np.array([3, 7, 11])
n = tot // sequence.sum() + 1
seq45 = np.tile(sequence, n)

Note that this is not exactly equivalent, since it repeats the full sequence, not individual elements, and seq45 may thus be slightly larger.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.