8

We can write a piece of python code and put it in already compiled ".pyc" file and use it. I am wondering that is there any kind of gain in terms of performance or it is just a kind of modular way of grouping the code.

Thanks a lot

Shan
  • 18,563
  • 39
  • 97
  • 132
  • 1
    possible duplicate of [If Python is interpreted, what are .pyc files?](http://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files) – pmr Feb 28 '12 at 16:43
  • For further reading, please refer to [why is the 'running' of .pyc files not faster compared to .py files?](https://stackoverflow.com/questions/16773362/why-is-the-running-of-pyc-files-not-faster-compared-to-py-files) – metatoaster Jan 10 '23 at 05:55

5 Answers5

10

There is no performance gain over the course of your program. It only improves the startup time.

A program doesn't run any faster when it is read from a ‘.pyc’ or ‘.pyo’ file than when it is read from a ‘.py’ file; the only thing that's faster about ‘.pyc’ or ‘.pyo’ files is the speed with which they are loaded.

http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html

And pyo files being the next optimization just remove doc strings from your code.

darksky
  • 1,955
  • 16
  • 28
jdi
  • 90,542
  • 19
  • 167
  • 203
  • 2
    .pyo files have assert statements stripped out (and potentially other optimisation) but might not have doc strings removed; it depends whether you use '-O' or '-OO'. – Scott Griffiths Feb 28 '12 at 17:51
  • Right, thanks. Yes its also assert statements, and it does depend on the level of optimization you pass. Overall though, its still a minimal runtime improvement. – jdi Feb 28 '12 at 18:25
  • @ealeon no idea about concrete numbers. It just may provide a benefit to startup time of a program when many py files don't have to be parsed and compiled to bytecode – jdi Oct 24 '17 at 18:23
  • 1
    right, but w/o numbers how do i justify creating .pyc or turning it off :P – ealeon Oct 24 '17 at 19:08
  • 2
    You test and measure, yourself, instead of asking for benchmarks to drive your decision :P – jdi Oct 24 '17 at 19:10
1

I'm not sure about .pyc files (very minor gain is at least not creating .pyc files again), but there's a '-O' flag for the Python interpreter which produces optimised bytecode (.pyo files).

Scott Griffiths
  • 21,438
  • 8
  • 55
  • 85
yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
1

Yes, simply because the first time you execute a .py file, it is compiled to a .pyc file. So basically you have to add the compilation time. Afterwards, the .pyc file should be always used.

jlengrand
  • 12,152
  • 14
  • 57
  • 87
0

You will gain the fraction of a second it took the Python runtime to parse and compile the source into bytecode, which only happens on first run/import.

You will lose portability.

See here.

A program doesn't run any faster when it is read from a ‘.pyc’ or ‘.pyo’ file than when it is read from a ‘.py’ file; the only thing that's faster about ‘.pyc’ or ‘.pyo’ files is the speed with which they are loaded.

lunixbochs
  • 21,757
  • 2
  • 39
  • 47
0

Based on the accepted answer of this question: Why are main runnable Python scripts not compiled to pyc files like modules?.

When a module is loaded, the py file is "byte compiled" to pyc files. The time stamp is recorded in pyc files. This is done not to make it run faster but to load faster

Community
  • 1
  • 1
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149