过多的代码注释会减慢执行速度吗

过多的代码注释会减慢执行速度吗

本文介绍了过多的代码注释会减慢执行速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果正确注释了超过75%的代码行,在执行大型.py文件时,性能会明显下降吗?

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?

推荐答案

运行python时,第一步是转换为字节码,这就是那些 .pyc 文件。注释已从其中删除,因此无关紧要。

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.

如果使用 -O -OO 选项,python将生成优化的 pyo 文件,这些文件可以忽略不计,甚至更快。主要区别在于:

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:



  • 使用 -OO 选项,将除去 __ doc __ 字符串。考虑到有时需要这些,建议不要使用 -OO 运行。

  • 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.

以下指出c $ c> .pyc 文件仅为模块保存。因此,每次执行时都必须重新编译顶级可执行文件。此步骤可以降低 massive python可执行文件的速度。在实践中,大多数代码应驻留在模块中,因此这不是问题。

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.

这篇关于过多的代码注释会减慢执行速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:42