Possible Duplicate:
Do comments slow down an interpreted language?
Will there be noticeable performance degradation in the execution of a large .py file if more than 75% of the lines of code are properly commented?
Possible Duplicate:
Do comments slow down an interpreted language?
Will there be noticeable performance degradation in the execution of a large .py file if more than 75% of the lines of code are properly commented?
No
When you run python, the first step is to convert to bytecode, which is what those .pyc
files are. Comments are removed from these, so it won't matter*.
If you run with the -O
or -OO
option, python will produce "optimized" pyo
files, which are negligibly faster, if faster at all. The main difference is that:
-O
assertion are removed,-OO
option, the __doc__
strings are stripped out. Given that those are sometimes needed, running with -OO
isn't recommended. * it's been pointed out below that .pyc
files are only saved for modules. Thus the top-level executable must be recompiled every time it's run. This step could slow down a massive python executable. In practice, most of the code should reside in modules, making this a non-issue.
.pyc
files, this is a one time cost, not a per-run cost. –
Fairminded .py
file invoked directly from the command line, it's trivial to write a "wrapper" for it that simply imports the big script. Then the big script gets compiled to a .pyc
and only the one-line wrapper is parsed each time it's run. –
Rump -O
removes assertions and sets __debug__ = False
, but it does not remove docstrings, only -OO
does that. Running with -O
is thus only harmful when you're debugging (or misusing assertions, which some codebases do). –
Ortolan © 2022 - 2024 — McMap. All rights reserved.