Question

When it comes to optimization of Python code (or any code), it often ends up coming down to profiling to optimize bottlenecks or slow functions. However, when optimizing these areas of code, are there any use cases for using pure Python bytecode inline? I know you can do this through the use of the compile built-in function and compiler module.

Was it helpful?

Solution

... are there any use cases for using pure Python bytecode inline?

Yes. Sometimes you can handroll a little faster code than Python normally generates on its own.

Also, you can gain access to the loop induction variables for list comprehensions.

Here are some links to get you started: https://www.google.com/search?q=python+bytecode+hacks

If you would like to make the bytecode manipulations programmatically, here is an optimization recipe that shows how to go about it: Decorator for BindingConstants at compile time

That said, if you care about speed, the simplest speed-up is often to run pypy instead of cpython if your application allows it.

OTHER TIPS

No. The source code is compiled to bytecode only once, when the module is first loaded. The bytecode is what is interpreted at runtime. So even if you could put bytecode inline into your source, it would at most only affect the startup time of the program by reducing the amount of time Python spent converting the source code into bytecode. It wouldn't actually change how fast that code runs. For instance, a "pure bytecode" version of a loop that runs 1000 times wouldn't run any faster than the same loop written in Python source code. See this similar question and also this one.

compile and compiler exist to let you dynamically create new executable source code from strings while the program is running. This isn't for a performance benefit, it's just because there's no other way to do it. When you run a Python program, Python compiles its source code to bytecode. But if you want to dynamically create new functions that aren't directly present in the source code (e.g., by mixing and matching code fragments, or allowing users to type in code while the program is running), you need a way to compile them on the fly, and that's what compile is for. (This is actually not a common need, so those functions aren't used that often.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top