python python优化模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2055557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:40:30  来源:igfitidea点击:

python optimized mode

python

提问by zaharpopov

Python can run script in optimized mode (-O) that turns off debugs like assert and if I remember also remove docstrings. I have no seen it used really and maybe it is just artifact of the past times. Is it being used? What for?

Python 可以在优化模式 (-O) 下运行脚本,关闭断言之类的调试,如果我记得还可以删除文档字符串。我没有看到它被真正使用过,也许它只是过去时代的产物。它被使用了吗?做什么的?

Why isn't this useless thing being removed in Python 3?

为什么 Python 3 中没有删除这个无用的东西?

回答by tzot

python -O does the following currently:

python -O 当前执行以下操作:

  • completely ignores asserts
  • sets the special builtin name __debug__to False (which by default is True)
  • 完全忽略断言
  • 将特殊的内置名称设置__debug__为 False(默认为 True)

and when called as python -OO

当被称为 python -OO

  • removes docstrings from the code
  • 从代码中删除文档字符串

I don't know why everyone forgets to mention the __debug__issue; perhaps it is because I'm the only one using it :) An if __debug__construct creates no bytecode at all when running under -O, and I find that very useful.

不知道为什么大家都忘了提这个__debug__问题;也许是因为我是唯一一个使用它的人:)if __debug__在 下运行时-O,构造根本不创建字节码,我发现这非常有用。

回答by Alex Martelli

It saves a small amount of memory, and a small amount of disk space if you distribute any archive form containing only the .pyofiles. (If you use asserta lot, and perhaps with complicated conditions, the savings can be not trivial and can extend to running time too).

如果您分发任何仅包含.pyo文件的存档表单,它会节省少量内存和少量磁盘空间。(如果您使用assert很多,并且可能具有复杂的条件,则节省的费用可能不是微不足道的,并且也可以扩展到运行时间)。

So, it's definitely not useless-- and of course it's being used (if you deploy a Python-coded server program to a huge number N of server machines, why everwould you want to waste N * X bytes to keep docstrings which nobody, ever, would anyway be able to access?!). Of course it would be better if it saved even more, but, hey -- waste not, want not!-)

所以,它绝对不是无用的-当然,它的使用(如果部署一个Python编码服务器程序服务器计算机的数量庞大的N,为什么曾经你想浪费N * X字节,以保持文档字符串没人,曾经,无论如何都可以访问?!)。当然,如果能节省更多就更好了,但是,嘿——不要浪费,不要!-)

So it's pretty much a no-brainer to keep this functionality (which is in any case trivially simple to provide, you know;-) in Python 3 -- why add even "epsilon" to the latter's adoption difficulties?-)

因此,在 Python 3 中保留此功能几乎是不费吹灰之力的(在任何情况下,提供此功能都非常简单,您知道;-)--为什么还要为后者的采用困难添加“epsilon”?-)

回答by abbot

Prepacked software in different Linux distributions often comes byte-compiled with -O. For example, this if from Fedora packaging guidelinesfor python applications:

不同 Linux 发行版中的预打包软件通常使用 -O 进行字节编译。例如,如果来自Fedora的 Python 应用程序打包指南

In the past it was common practice to %ghost .pyo files in order to save a small amount of space on the users filesystem. However, this has two issues: 1. With SELinux, if a user is running python -O [APP] it will try to write the .pyos when they don't exist. This leads to AVC denial records in the logs. 2. If the system administrator runs python -OO [APP] the .pyos will get created with no docstrings. Some programs require docstrings in order to function. On subsequent runs with python -O [APP] python will use the cached .pyos even though a different optimization level has been requested. The only way to fix this is to find out where the .pyos are and delete them.

The current method of dealing with pyo files is to include them as is, no %ghosting.

在过去,%ghost .pyo 文件是一种常见的做法,以便在用户文件系统上节省少量空间。但是,这有两个问题: 1. 使用 SELinux,如果用户正在运行 python -O [APP],它会尝试编写不存在的 .pyos。这会导致日志中出现 AVC 拒绝记录。2. 如果系统管理员运行 python -OO [APP] .pyos 将被创建,没有文档字符串。一些程序需要文档字符串才能运行。在随后使用 python -O [APP] 运行时,即使请求了不同的优化级别,python 也会使用缓存的 .pyos。解决此问题的唯一方法是找出 .pyos 的位置并删除它们。

当前处理 pyo 文件的方法是按原样包含它们,没有 %ghosting。

回答by AndiDog

Removing assertions means a smallperformance benefit, so you could use this for "release" code. Anyway nobody uses it because many Python libraries are open sourced and thus the help()function should work.

删除断言意味着小的性能优势,因此您可以将其用于“发布”代码。无论如何没有人使用它,因为许多 Python 库是开源的,因此该help()函数应该可以工作。

So, as long as there isn't any realoptimization in this mode, you can ignore it.

所以,只要在这种模式下没有任何真正的优化,你就可以忽略它。