Python and the need for speed

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Wed Apr 12 07:56:22 EDT 2017


bart4858 at gmail.com writes:

> On Wednesday, 12 April 2017 10:57:10 UTC+1, bart... at gmail.com  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.

FYI, the question is not how to optimize the code but how to prevent the
programmer from writing stupid code in the first place. Someone
suggested that a language should do that.

But you appear to be talking about the original topic of the thread, as
seen on the subject line, so ok :)



More information about the Python-list mailing list