Skip to content Skip to sidebar Skip to footer

Why Is The 'running' Of .pyc Files Not Faster Compared To .py Files?

I know the difference between a .py and a .pyc file. My question is not about how, but about why According to the docs: A program doesn’t run any faster when it is read from a .

Solution 1:

When you run a .py file, it is first compiled to bytecode, then executed. The loading of such a file is slower because for a .pyc, the compilation step has already been performed, but after loading, the same bytecode interpretation is done.

In pseudocode, the Python interpreter executes the following algorithm:

code = load(path)
ifpath.endswith(".py"):
    code = compile(code)
run(code)

Solution 2:

The way the programs are run is always the same. The compiled code is interpreted.

The way the programs are loaded differs. If there is a current pyc file, this is taken as the compiled version, so no compile step has to be taken before running the command. Otherwise the py file is read, the compiler has to compile it (which takes a little time) but then the compiled version in memory is interpreted just like in the other way.

Post a Comment for "Why Is The 'running' Of .pyc Files Not Faster Compared To .py Files?"