python Python的基本优化模式有什么用?(蟒蛇-O)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1693088/
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 22:52:25  来源:igfitidea点击:

What is the use of Python's basic optimizations mode? (python -O)

pythonoptimizationassertbytecode

提问by u0b34a0f6ae

Python has a flag -Othat you can execute the interpreter with. The option will generate "optimized" bytecode (written to .pyo files), and given twice, it will discard docstrings. From Python's man page:

Python 有一个标志-O,你可以用它来执行解释器。该选项将生成“优化的”字节码(写入 .pyo 文件),并给出两次,它将丢弃文档字符串。从 Python 的手册页:

-O Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. Given twice, causes docstrings to be discarded.

-O 打开基本优化。这会将已编译(字节码)文件的文件扩展名从 .pyc 更改为 .pyo。给定两次,导致文档字符串被丢弃。

This option's two major features as I see it are:

我认为此选项的两个主要功能是:

  • Strip all assert statements. This trades defense against corrupt program state for speed. But don't you need a ton of assert statements for this to make a difference? Do you have any code where this is worthwhile (and sane?)

  • Strip all docstrings. In what application is the memory usage so critical, that this is a win? Why not push everything into modules written in C?

  • 剥离所有断言语句。这将防御损坏的程序状态以换取速度。但是,您不需要大量的断言语句才能有所作为吗?您是否有任何代码值得(并且理智?)

  • 去除所有文档字符串。在哪个应用程序中内存使用如此重要,以至于这是一个胜利?为什么不将所有内容都推送到用 C 编写的模块中?

What is the use of this option? Does it have a real-world value?

这个选项有什么用?它有现实世界的价值吗?

采纳答案by Ned Batchelder

On stripping assert statements: this is a standard option in the C world, where many people believe part of the definition of ASSERT is that it doesn't run in production code. Whether stripping them out or not makes a difference depends less on how many asserts there are than on how much work those asserts do:

关于剥离断言语句:这是 C 世界中的标准选项,许多人认为 ASSERT 的部分定义是它不在生产代码中运行。将它们剥离与否会产生不同,与其说取决于断言的数量,不如说取决于这些断言做了多少工作:

def foo(x):
    assert x in huge_global_computation_to_check_all_possible_x_values()
    # ok, go ahead and use x...

Most asserts are not like that, of course, but it's important to remember that you can do stuff like that.

当然,大多数断言不是那样的,但重要的是要记住你可以做那样的事情。

As for stripping docstrings, it does seem like a quaint holdover from a simpler time, though I guess there are memory-constrained environments where it could make a difference.

至于剥离文档字符串,它看起来确实像是从更简单的时代开始的古怪保留,尽管我猜在内存受限的环境中它可能会有所作为。

回答by tzot

Another use for the -Oflag is that the value of the __debug__builtin variable is set to False.

-O标志的另一个用途是将__debug__内置变量的值设置为False.

So, basically, your code can have a lot of "debugging" paths like:

因此,基本上,您的代码可以有很多“调试”路径,例如:

if __debug__:
     # output all your favourite debugging information
     # and then more

which, when running under -O, won't even be included as bytecode in the .pyofile; a poor man's C-ish #ifdef.

在 下运行时-O,它甚至不会作为字节码包含在.pyo文件中;一个穷人的 C-ish #ifdef。

Remember that docstrings are being dropped onlywhen the flag is -OO.

请记住,当标志为-OO.

回答by oefe

If you have assertions in frequently called code (e.g. in an inner loop), stripping them can certainly make a difference. Extreme example:

如果您在经常调用的代码中(例如在内部循环中)有断言,那么剥离它们肯定会有所作为。极端例子:

$ python    -c 'import timeit;print timeit.repeat("assert True")'
[0.088717937469482422, 0.088625192642211914, 0.088654994964599609]
$ python -O -c 'import timeit;print timeit.repeat("assert True")'
[0.029736995697021484, 0.029587030410766602, 0.029623985290527344]

In real scenarios, savings will usually be much less.

在实际情况下,储蓄通常会少得多。

Stripping the docstrings might reduce the size of your code, and hence your working set.

剥离文档字符串可能会减少代码的大小,从而减少您的工作集。

In many cases, the performance impact will be negligible, but as always with optimizations, the only way to be sure is to measure.

在许多情况下,性能影响可以忽略不计,但与优化一样,唯一可以确定的方法是测量。

回答by Mike Graham

I have never encountered a good reason to use -O. I have always assumed its main purpose is in case at some point in the future some meaningful optimization is added.

我从来没有遇到过使用-O. 我一直认为它的主要目的是在未来的某个时候添加一些有意义的优化。

回答by Nick Craig-Wood

I imagine that the heaviest users of -Oare py2exepy2appand similar.

我想最重的用户-Opy2exe py2app和类似的。

I've personally never found a use for -Odirectly.

我个人从未发现-O直接使用 for 。

回答by Jay P.

You've pretty much figured it out: It does practically nothing at all. You're almost never going to see speed or memory gains, unless you're severely hurting for RAM.

你已经很清楚了:它实际上什么都不做。除非 RAM 严重受损,否则您几乎永远不会看到速度或内存的提升。

回答by z0r

But don't you need a ton of assert statements for this to make a difference? Do you have any code where this is worthwhile (and sane?)

但是,您不需要大量的断言语句才能有所作为吗?您是否有任何代码值得(并且理智?)

As an example, I have a piece of code that gets paths between nodes in a graph. I have an assert statement at the end of the function to check that the path doesn't contain duplicates:

例如,我有一段代码可以获取图中节点之间的路径。我在函数末尾有一个 assert 语句来检查路径是否不包含重复项:

assert not any(a == b for a, b in zip(path, path[1:]))

I like the peace of mind and claritythat this simple statement gives during development. In production, the code processes some big graphs and this single line can take up to 66% of the run time. Running with -Otherefore gives a significant speed-up.

我喜欢这个简单的语句在开发过程中带来的安心和清晰。在生产中,代码处理一些大图,而这一行可能占用高达 66% 的运行时间。-O因此,运行会带来显着的加速。