To improve
the performance of loading python modules, python caches the compiled version
of every module that you use.
For example
arithmetic.py
def sum(a, b): return a+b def subtract(a, b): return a-b def mul(a,b): return a*b def div(a, b): return a/b
main.py
import arithmetic if __name__ == "__main__": import sys a = int(sys.argv[1]) b = int(sys.argv[2]) print("a = ", a, "b = ", b) print(arithmetic.sum(a,b)) print(arithmetic.subtract(a,b)) print(arithmetic.mul(a,b)) print(arithmetic.div(a,b))
$ ls arithmetic.py main.py $ $ python3 main.py 10 20 a = 10 b = 20 30 -10 200 0.5 $ $ ls __pycache__ arithmetic.py main.py $ $ cd __pycache__/ $ ls arithmetic.cpython-35.pyc
Observe above output, python creates __pycache__
directory and caches the compiled version of arithmetic.py file. The compiled
version of python files follows the format 'module.version.pyc'. In my case, I
am using python 3.5, so the file name is arithmetic.cpython-35.pyc. This naming
convention allows compiled modules from different releases and different
versions of Python to coexist. Major advantage of these compiled modules is
that, these are platform independent, so you can ship these to other platforms
and use as it is.
Note
A program doesn’t run any faster when it
is read from a .pyc file than when it is read from a .py file; the only thing
that’s faster about .pyc files is the speed with which they are loaded.
No comments:
Post a Comment