Python and the need for speed

bart4858 at bart4858 at
Wed Apr 12 07:48:05 EDT 2017

On Wednesday, 12 April 2017 10:57:10 UTC+1, bart... at  wrote:
> On Wednesday, 12 April 2017 07:48:57 UTC+1, Steven D'Aprano  wrote:

> >     for i in range(10):
> >         answer += 1

> > So... how exactly does the compiler prohibit stupid code?

> And this lookup happens for every loop iteration.

I've just looked at byte-code for that loop (using an on-line Python as I don't have it on this machine). I counted 7 byte-codes that need to be executed per iteration, plus five to set up the loop, one of which needs to call a function.

My language does the same loop with only 4 byte-codes. Loop setup needs 2 (to store '10' into the loop counter).

It also has the option of using a loop with no control variable (as it's not used here). Still four byte-codes, but the looping byte-code is a bit faster.

Plus there is the option of using ++answer instead of answer += 1. Now there are only two byte-codes! (NB don't try ++ in Python.)

These are straightforward language enhancements.


More information about the Python-list mailing list