0

I'm looking for a more efficient way to compute this (searched for something similar to numpy's arange):

R = 0
l1 = []
gamma = 0.99
x = 12
for i in range(0, 1000):       
    R = x - (1-gamma) * R
    l1.append(R)

the iteration and appending are too slow

oshi2016
  • 875
  • 2
  • 10
  • 20
  • See if [this answer](https://stackoverflow.com/a/47429565/3005167) gives you a few ideas. – MB-F Jan 05 '18 at 08:01
  • I'm a bit confused by your code. Creating a list of 1000 values using the loop in your code completes very quickly. All but the first few values are the same (due to floating point precision, I suspect, otherwise they'd continue to converge on some real number). If you're using a different calculation in your real code, please show it. There might be a way to compute the same results using a closed-form formula. – Blckknght Jan 05 '18 at 22:20

1 Answers1

1

You can get an easy factor of 3 if you JIT the function using Numba:

Numba JIT

You should also explore Numpy and play around in a notebook or IPython to do some performance tests depending on your typical input and come back if you have more specific questions.

Just a small update: With Julia (https://julialang.org) I get something around 2.5us, so there is still room for improvement ;)

Using Julia

tamasgal
  • 24,826
  • 18
  • 96
  • 135