3

I searched around for a while and have found a number of reasonable claims that CPython's compilation allows faster execution of Python code. I was wondering, though, if anyone knows of any benchmarks demonstrating the degree of the speedup.

Alternatively, perhaps there's an easy way for me to benchmark it. Is there a Python flag that can be given at runtime to turn off compilation?

smci
  • 32,567
  • 20
  • 113
  • 146
Translunar
  • 3,739
  • 33
  • 55
  • 2
    There's no flag; you'd have to rewrite a big part of the interpreter to get it to interpret the AST directly (and you'd have to rewrite even more if you're thinking of having it interpret the source without parsing it into an AST). – user2357112 Jul 18 '13 at 02:55
  • Do you mean Cython? When taking about interpreters, an unqualified "Python" basically means CPython (vs. IronPython, Jython, PyPy, etc.). – Nick T Jul 18 '13 at 03:16
  • 2
    @NickT, how is my question unclear? I specified CPython, not Cython. – Translunar Jul 18 '13 at 04:07
  • At least as far as [disabling bytecode caching goes, I've never heard of it being necessary or useful, and neither have the audience of HN, other than versions < 2.7](https://news.ycombinator.com/item?id=23366924). And [back in Python 3.5 /PEP 488 removed .pyo files](https://docs.python.org/3/whatsnew/3.5.html#pep-488-elimination-of-pyo-files). Given noone's reported any anecdotal evidence of speedup on general code, can you please close this question and edit the premise of the title to avoid propagating misinformation? (Also: which version(s) you were talking about: 2013 is pretty ancient). – smci Dec 15 '20 at 04:43
  • If the short answer to *"Is there a runtime option to run Python with bytecode compilation disabled?"* is *"No there's no such switch"* then you can't even readily measure this, unless you create your own custom build (which apparently noone can be bothered to do), so the premise of the question is pretty moot. Compiler peoplle could give estimates. Or you could get an idea by [comparing 2.7, 2.8, 3.5, 3.9 on the same benchmark code](https://pythonspeed.com/performance/) so you can see how much improvement recent changes made. – smci Dec 15 '20 at 04:48

1 Answers1

4

All code run by cpython must be compiled to bytecode before it can be run. That's just how the interpreter works, and you probably can't reasonably change this (without writing your own interpreter).

However, by default the compiled bytecode for modules that are loaded will be cached in .pyc files. This means it won't need to be compiled again the next time you load it. Bytecode caching is probably what you heard about, as it can speed up the importing of previously used modules by a fair amount. It doesn't change performance after the modules are loaded though.

You can disable bytecode caching with the -B command line option or the PYTHONDONTWRITEBYTECODE environment variable. If you wanted to do a test of the speed difference, you may also need to delete any existing cache. In Python 2, the compiled bytecode would be written to a .pyc file right next to the .py souce file. In Python 3, this was changed to use a __pycache__ folder which can hold several .pyc files from different versions of Python (so you could have several cached versions at once, see PEP 3147 for more details).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • 4
    Thanks! This is very helpful. Do you know of any benchmarks, though, that demonstrate the speedup granted by bytecode compilation? Surely comparisons were made when they first tested the feature, yes? – Translunar Jul 18 '13 at 06:08