Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When a module is loaded, the py file is "byte compiled" to pyc files. The time stamp is recorded in pyc files. <strong>This is done not to make it run faster but to load faster.</strong> Hence, it makes sense to "byte compile" modules when you load them.</p> <ul> <li><a href="http://docs.python.org/tutorial/modules.html#compiled-python-files" rel="noreferrer">http://docs.python.org/tutorial/modules.html#compiled-python-files</a></li> </ul> <p>[Edit : To include notes, references]</p> <blockquote> <p>From PEP 3147 on "Byte code compilation":</p> </blockquote> <p>CPython compiles its source code into "byte code", and for performance reasons, it caches this byte code on the file system whenever the source file has changes. This makes loading of Python modules much faster because the compilation phase can be bypassed. When your source file is foo.py, CPython caches the byte code in a foo.pyc file right next to the source.</p> <blockquote> <p>How byte code compiled files are tracked with respect to Python version and "py" file changes:</p> </blockquote> <p>It also inserts a magic number in the compiled byte code ".pyc" files. This changes whenever Python changes the byte code format, usually in major releases.<br> This ensures that pyc files built for previous versions of the VM won't cause problems. The timestamp is used to make sure that the pyc file match the py file that was used to create it. When either the magic number or timestamp do not match, the py file is recompiled and a new pyc file is written.</p> <p>"pyc" files are not compatible across Python major releases. When Python finds a pyc file with a non-matching magic number, it falls back to the slower process of recompiling the source.</p> <p>Thats the reason, if you simply distribute the ".pyc" files compiled for the same platform will not work any more, if the python version changes.</p> <blockquote> <p><em>In Nutshell</em></p> </blockquote> <p>If there is a byte compiled file ".pyc" and it's timestamp indicates that it is recent then it will be loaded up other wise python will fallback on the slower approach of loading the ".py" files. The execution performance of the ".py" file is not affected but the loading of the ".pyc" files is faster than ".py" files.</p> <p>Consider executing a.py which imports b.py </p> <pre><code>Typical total performance = loading time (A.py) + execution time (A.py) + loading time (B.py) + execution time (B.py) Since loading time (B.pyc) &lt; loading time (B.py) You should see a better performance by using the byte compiled "pyc" files. </code></pre> <p><em>That said, if you have a large script file X.py, modularizing it and moving contents to other modules results in taking advantage of lower load time for byte code compiled file.</em></p> <p>Another inference is that modules tend to be more stable than the script or the main file. Hence it is not byte compiled at all. </p> <blockquote> <p>References</p> </blockquote> <ul> <li><a href="http://effbot.org/zone/python-compile.htm" rel="noreferrer">http://effbot.org/zone/python-compile.htm</a></li> <li><a href="http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html" rel="noreferrer">http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload