views:

71

answers:

2

I've heard that some types of runtime compilation aren't a performance hit. The official Python documentation asserts that running a .pyc file is no faster than .py. (I can't find the link right now.) How is this possible? Doesn't it take time to generate bytecode from the .py?

What about languages like PHP? Isn't it necessary to compile them on every execution? Why would you want this? It seems like a waste of time if the same script will be run many times.

+4  A: 

Without the context of the actual document you were reading, it's impossible to say for sure, but I think you might be misunderstanding what is meant by "performance".

The application starts slower if it has to be compiled to bytecode on startup, but it doesn't run slower. Without qualification, someone talking about "performance" is almost never referring to start-up time.

Nicholas Knight
+1  A: 

Python checks for a .pyc file and when it does not exist, or if it is out of date it will recreate the bytecode from the .py file.

If the filesystem is not writeable, this step still happens in RAM before the program runs.

So Python is never interpreting code line by line as it runs the program.

The upshot is that if the .pyc file exists and is current, you program will start running a tiny bit sooner, but the execution speed will be the same

gnibbler