nested loops in Python

Christian Gollwitzer auriocus at gmx.de
Thu Sep 8 13:04:24 EDT 2016


Am 08.09.16 um 12:20 schrieb Igor Kozin:
> Why nested loops are so slow in Python? Is it because new contexts are created?
> For more details, see
> http://stackoverflow.com/questions/26611043/numpy-vs-cython-nested-loop-so-slow
> http://stackoverflow.com/questions/39371021/efficient-loop-over-numpy-array

The answers are right there, too - what are you mising? CPython is an 
interpreter. For every seemingly simple operation like a[i]=j, a 
function pointer is called, the variables contain polymorphic types 
which are morphed, etc. The same thing in compiled code is a single 
machine instruction. Speed differences of ~100x are normal between 
compiled and interpreted code over all dynamic languages.

	Christian




More information about the Python-list mailing list