Will excessive commenting of code slow execution? [duplicate]

限于喜欢 提交于 2019-12-04 00:30:23

问题


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?


回答1:


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:

  • with -O assertion are removed,
  • with the -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.



来源:https://stackoverflow.com/questions/10486666/will-excessive-commenting-of-code-slow-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!