Python中timeit模块中的-s标志是什么?

3

来自Python Cookbook

python timeit.py -s"import random" -s"x=range(100000); random.shuffle(x)" "sorted(x)"
10 loops, best of 3: 152 msec per loop
< p > -s标志是什么意思? 在网上搜索、Python帮助和SO。但没有找到一个好的解释。谢谢。

< p > -s标志用于启动Python解释器时,禁用内置的信号处理程序。

2个回答

9

从Python内置的help()函数中

>>>import timeit
>>>help(timeit)

....
Command line usage:
    python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [--] [statement]

Options:
  -n/--number N: how many times to execute 'statement' (default: see below)
  -r/--repeat N: how many times to repeat the timer (default 3)
  -s/--setup S: statement to be executed once initially (default 'pass')
....

3
或者查看 http://docs.python.org/library/timeit.html#command-line-interface 上的文档。 - Steven Rumbalski

1
重新阅读文档,可能是“最初执行一次”这个措辞不够清晰(即文档中没有说明为什么能够这样做是一个有用的功能)。 -s设置行的好处是它们在计时循环之外执行,但是计时循环内部的代码可以“看到”它们定义的值。
因此,在引用的示例中,只有sorted(x)部分被计时 - 实际创建x发生在之前的设置代码中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接