Why is the 'running' of .pyc files not faster compared to .py files?

后端 未结 2 1316
甜味超标
甜味超标 2020-12-17 14:58

I know the difference between a .py and a .pyc file. My question is not about how, but about why Accordin

2条回答
  •  不思量自难忘°
    2020-12-17 15:46

    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)
    if path.endswith(".py"):
        code = compile(code)
    run(code)
    

提交回复
热议问题