I have a loop that is my biggest time suck for a particular function and I’d like to speed it up. Current, this single loop takes up about 400ms, while the execution for the rest of the function takes about 610ms.
The code is:
for ctr in xrange(N):
list1[ctr] = in1[ctr] - in1[0] - ctr * c1
list2[ctr] = in2[ctr] - in2[0] - ctr * c2
list3[ctr] = c3 - in1[ctr]
list4[ctr] = c4 - in2[ctr]
N can be anywhere from around 40,000 to 120,000, and is the length of all lists (in1, in2, listN) shown.
Does anyone know some Python tricks to speed this up? I already tried using map instead, as I know it tries to compile to more efficient code, but it was about 250ms slower.
Thanks
Assuming that
list1,list2, etc, all are numerical, consider using numpy arrays instead of lists. For large sequences of integers or floats you’ll see a huge speedup.If you go that route, your loop above could be written like this:
And as a full stand-alone example for timing:
Of course, if your
list1,list2, etc are non-numerical (i.e. lists of python objects other than floats or ints), then this won’t help.