从翻译,我得到:
>>> timeit.repeat("-".join( str(n) for n in range(10000) ) , repeat = 3, number=10000)
[1.2294530868530273, 1.2298660278320312, 1.2300069332122803] # this is seconds
Run Code Online (Sandbox Code Playgroud)
从命令行,我得到:
$ python -m timeit -n 10000 '"-".join(str(n) for n in range(10000))'
10000 loops, best of 3: 1.79 msec per loop # this is milli second
Run Code Online (Sandbox Code Playgroud)
为什么这两种情况下的时间量差异如此?
当尝试使用Python内置模块'timeit'时,如下所示:
timeit.Timer('print "hi"').timeit()
Run Code Online (Sandbox Code Playgroud)
它打印多行; 这是为什么?它不断印刷"喜":
hi
hi
hi
hi
...
Run Code Online (Sandbox Code Playgroud) 其中两个语句运行而另一个语句因语法错误而失败.我究竟做错了什么?
>>> Timer('for i in xrange(10): oct(i)').repeat(3)
[2.7091379165649414, 2.6934919357299805, 2.689150094985962]
>>> Timer('n = [] ; n = [oct(i) for i in xrange(10)]').repeat(3)
[4.0500171184539795, 3.6979520320892334, 3.701982021331787]
>>> Timer('n = [] ; for i in xrange(10): n.append(oct(i))').repeat(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/timeit.py", line 136, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 6
n = [] ; for i in xrange(10): n.append(oct(i))
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud) 我得到了以下两个python timeit行的不同结果.
print(min(timeit.repeat(lambda: 1+1)))
print(min(timeit.repeat('lambda: 1+1')))
Run Code Online (Sandbox Code Playgroud)
输出类似于:
0.13658121100002063
0.10372773000017332
Run Code Online (Sandbox Code Playgroud)
你能帮忙解释一下它们之间的区别吗?
我是Python的新手,正在尝试绘制两个函数的计算速度。例如,定义了两个函数(见下文),如何使用IPython / Jupyter中的timeit函数返回每次迭代的时间?
def func1(x) :
return x*x
def func2(x) :
return x+x
%timeit for x in range(100) : func1(x)
%timeit for x in range(100) : func2(x)
Run Code Online (Sandbox Code Playgroud)
我阅读了https://ipython.org/ipython-doc/3/interactive/magics.html,可以使用'-o'返回“可以存储在变量中的TimeitResult,以更详细地检查结果”。
但是,如何将其保存到变量“ func1_time”中?如何读取每次迭代的时间?我的目标是为两个函数绘制x与时间的关系。
任何帮助都感激不尽。谢谢。
我正在尝试ipython使用
%timeit魔术函数多次运行特定测试。出于演示目的,我将-n1在-n3这里使用代替
,并使用一个简单的print(1)函数。
该%%timeit和%timeit帮助表示如下:
Options: -n<N>: execute the given statement <N> times in a loop. If this
value is not given, a fitting value is chosen.
-r<R>: repeat the loop iteration <R> times and take the best result.
Default: 3 (the 3 here is a typo in ipython, for which I have submitted a
PR)
Run Code Online (Sandbox Code Playgroud)
但是,如果我执行以下操作:
%%timeit -n1
print(1)
Run Code Online (Sandbox Code Playgroud)
或者
%timeit -n1 print(1)
Run Code Online (Sandbox Code Playgroud)
它实际上1连续打印7 次,如下所示 …
我正在尝试安装 timeit,但这就是我得到的:
$ sudo pip install timeit
Downloading/unpacking timeit
Could not find any downloads that satisfy the requirement timeit
No distributions at all found for timeit
Storing complete log in /Users/username/.pip/pip.log
Run Code Online (Sandbox Code Playgroud) 我一直在使用Python timeit模块,但它只是通过交互式Python会话或Unix shell.现在,我正在尝试在Windows命令提示符(cmd.exe)中测量一些代码片段,但它显示以下错误:
C:\Users\Me>python -m timeit '"-".join(map(str, range(100)))'
Traceback (most recent call last):
File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python33\lib\runpy.py", line 73, in _run_code
exec(code, run_globals)
File "C:\Python33\lib\timeit.py", line 334, in <module>
sys.exit(main())
File "C:\Python33\lib\timeit.py", line 298, in main
t = Timer(stmt, setup, timer)
File "C:\Python33\lib\timeit.py", line 131, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 6
'-.join(map(str,
^
SyntaxError: EOL while scanning string literal
Run Code Online (Sandbox Code Playgroud)
这是相当令人困惑的,因为我没有在字符串中插入任何换行符 - 相反,我实际上直接从timeit模块文档粘贴了示例.
在玩这个时,我尝试测试没有任何空格的片段,因为错误标记了它们之前的字符.即使现在没有异常,模块也会报告相同的执行时间,就像我传递了一个pass …
我正在尝试编写一个简单的时间装饰器来测量函数所用的时间.但是下面的代码给出了我们的递归错误.它出什么问题了?
import timeit
def measure(func):
def wrapper():
func_name = func.__name__
setup="from __main__ import {}".format(func_name)
op_time = timeit.timeit('{}()'.format(func_name), number = 2, setup=setup)
print(ot)
return wrapper
@measure
def sample():
return 10
sample()
Run Code Online (Sandbox Code Playgroud)
产量
RecursionError Traceback (most recent call last)
<ipython-input-61-e079e1bd7fba> in <module>()
15 return 10
16
---> 17 sample()
<ipython-input-61-e079e1bd7fba> in wrapper()
7 func_name = func.__name__
8 setup="from __main__ import {}".format(func_name)
----> 9 op_time = timeit.timeit('{}()'.format(func_name), number = 2, setup=setup)
10 print(ot)
11 return wrapper
~/anaconda3/lib/python3.6/timeit.py in timeit(stmt, setup, timer, number, globals) …Run Code Online (Sandbox Code Playgroud) 所以我对 Rust 还很陌生,但来自 Python 的我发现这种情况总体来说非常令人困惑。
我喜欢 Python,因为如果你想对一段代码或只是一个函数调用进行计时,它非常容易:
print(timeit('a = "hee hee la le dah"; my_awesome_fn()', number = 1_000, globals=globals()))
Run Code Online (Sandbox Code Playgroud)
然后只需调用python script.py,或者更好的是,只需使用 IDE 中的绿色“运行”按钮即可调用脚本。但我在 Rust 中找不到等效的功能。
我知道 Rust 生态系统中有一个称为基准测试的概念,并且有一些类似的库就是criterion为此目的而存在的。问题是我对高等数学和统计学一无所知(本质上可以把我当作一个无能的白痴),而且我怀疑我能否从这样的框架或工具中受益匪浅。
所以我只是好奇如何tests在 Cargo 中使用来测试 Rust 中的代码块,甚至更好,甚至是函数调用。
例如,假设我在 Rust 中有类似的函数,我想多次调用它,然后检查性能如何变化等:
pub fn my_awesome_fn() {
trace!("getting ready to do something cool...");
std::thread::sleep(std::time::Duration::from_millis(500));
info!("finished!");
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能简单地在 Rust 中计时这个函数my_awesome_fn?我想我正在寻找类似timeitpython 或类似的东西。理想情况下,它应该是直接使用的,并假设我对我正在做的事情一无所知。我很好奇是否有一个现有的库或框架可以用于此目的。
timeit ×10
python ×9
ipython ×2
cmd ×1
command-line ×1
cpython ×1
decorator ×1
performance ×1
pip ×1
python-3.x ×1
rust ×1
time ×1
windows ×1