Is there any performance gain when using .pyc files in python?

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

thanks a lot

+6
source share
5 answers

There is no increase in performance during your program. This only improves startup time.

A program does not start faster when it is read from '.pyc or' .pyo than when it is read from a .py file; the only thing faster about .pyc or .pyo files is the speed at which they load.

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

And the pyo files, which are the next optimization, just remove the document lines from your code.

+7
source

I'm not sure about the .pyc files (a very small gain, at least not creating the .pyc files again), but there is an -O flag for the Python interpreter that creates optimized bytecode (.pyo files).

+1
source

You will get a split second when it took 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 does not start faster when it is read from '.pyc or' .pyo than when it is read from a .py file; the only thing faster about .pyc or .pyo files is the speed at which they load.

0
source

Yes, simply because the first time you run the .py file, it is compiled into a .pyc file. So basically you should add compile time. After that, the .pyc file should always be used.

0
source

Based on the accepted answer to this question: Why are the main Python executables not compiled into pyc files, such as modules? .

When the module is loaded, the py byte is compiled into pyc files. The timestamp is written to pyc files. This is not done in order to make it work faster, but load faster

0
source

Source: https://habr.com/ru/post/909561/


All Articles