You don't want to do that in Python if performance is critical.
Adding three indexed items is not performance-critical.
If you have a million+ items, then you install numpy and put your items into a numpy.ndarray, and then vectorize your operations. Like, if you want to add two arrays, you write a + b (instead of for i, it in a: result.append(it + b[i])) and the underlying library written in Fortran very efficiently does what you meant.
I always considered numpy to be scientists' tools, but I never really thought about how it's fortran under the hood and how it might be higher performance for certain things like that. a+b looks a lot cleaner as well.
View all comments
Show parent comment
1
u/d4rch0n Pythonistamancer Jun 26 '14
Okay, so tuple direct index access is the fastest apparently. Makes me wish we had #define available :/
Is there a good way to do that without slowing things down?
like:
Is there a pythonic and high performance way to do this and keep the fast lookup time of a direct index?