Python and the need for speed

bart4858 at bart4858 at
Wed Apr 12 19:30:38 EDT 2017

On Thursday, 13 April 2017 00:09:35 UTC+1, Gregory Ewing  wrote:
> bart4858 at wrote:
> > But the language can also play a part in not allowing certain things to be
> > expressed naturally. So the for-loop in the example has to have a
> > control-variable even if it's not referenced.
> If the compiler can recognise when code is "stupid", it's
> probably capable of doing something more intelligent than
> just rejecting it outright.
> E.g. it could notice that a loop variable wasn't used
> anywhere in the function and optimise it out. And it could
> recognise x += 1 and emit an ADD_ONE bytecode for it.
> Etc.

Maybe. (Although I think Python would have difficulty in turning x+=1 into a single opcode, if using normal object references and a shared object model.)

But I wouldn't be happy about a language deciding my code is 'stupid'. I might for example have an N-times loop containing x+=1 because I want to know how long it takes to execute x+=1 N times. (This is a common problem with benchmarking code actually, as you can end up measuring how long it takes /not/ to do something instead of doing it!)

Anyway that loop example (I think it was Steve's) was a simple illustration. It will usually be harder to figure out if a bit of code is rational.

Even with x+=1, isn't it possible to override the + or += operation with user code? Code which change the handling method at each iteration. This is dynamism making a rod for its own back. 99.99...% of the time that won't happen, but...


More information about the Python-list mailing list