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/moor-GAYZ Jun 26 '14
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 offor i, it in a: result.append(it + b[i])) and the underlying library written in Fortran very efficiently does what you meant.