有人可以从 'Learning Python' 解释这个基准测试函数吗
Could someone explain this benchmarking function from 'Learning Python'
我正在通过 Mark Lutz 的书 'Learning Python' 学习 python。我有一个关于这个基准测试功能的问题。我了解前两个功能的工作原理,但我不太了解最后一个功能。有人可以向我解释最后一个函数的确切工作原理及其结果 returns 吗?根据作者的说法,它是前两个功能的组合。它 returns 最好的总次数 - 在指定数量的运行中计算调用特定函数 x 次的时间的最少时间。我理解他的解释,但我不理解元组 the bestoftotal 函数 returns。谁能给我解释一下这个元组?
# File timer.py
"""
Homegrown timing tools for function calls.
Does total time, best-of time, and best-of-totals time
"""
import time, sys
try:
timer = time.perf_counter
except AttributeError:
timer = time.clock if sys.platform[:3] == 'win' else time.time
def total(reps, func, *pargs, **kargs):
"""
Total time to run func() reps times.
Returns (total time, last result)
"""
repslist = list(range(reps))
start = timer()
for i in repslist:
ret = func(*pargs, **kargs)
elapsed = timer() - start
return (elapsed, ret)
def bestof(reps, func, *pargs, **kargs):
"""
Quickest func() among reps runs.
Returns (best time, last result)
"""
best = 2 ** 32
for i in range(reps):
start = timer()
ret = func(*pargs, **kargs)
elapsed = timer() - start
if elapsed < best: best = elapsed
return (best, ret)
def bestoftotal(reps1, reps2, func, *pargs, **kargs):
"""
Best of totals:
(best of reps1 run of (total of reps2 run of func))
"""
return bestof(reps1, total, reps2, func, *pargs, **kargs)
bestoftotal
returns 以 total
作为 func 参数调用函数 bestof
。
前两个函数你说你看懂了,为了我自己的理解,我再解释一下。
所有的 *pargs 和 **kargs 都会让事情变得有点混乱。为简化起见,假设我想要 benchark myFunction()
,它不带任何参数。
为了测量 运行 myFunction
10 次所花费的总时间,我这样做:
total(10, myFunction)
为了衡量 5 次试验中最好的(1 次试验 = myFunction
中的 1 运行),我这样做:
bestof(5, myFunction)
现在,我想知道 5 次试验中最好的一次,一次试验由 运行 函数 10 次 ("the best of total trials") 组成。
一试其实就是函数total(10, myFunction)
的一个运行
所以 5 次试验中最好的是 bestof(5, total(10,my Function))
最后一行在 Python 中不是有效的调用。这就是 *pargs 和 **kwargs 有用的地方。
当您调用 bestof(5, total, 10, myFunction)
时,参数 10 和 myFunction
会在调用 total
:
时传递
ret = func(*pargs, **kargs)
行变为 ret = total(10, myFunction)
.
那么 bestoftotal
是做什么的呢?好吧,它需要两个数字 reps1 和 reps 2(在我的例子中是 5 和 10),以及要进行基准测试的函数。它 returns 调用函数 bestof
.
bestoftotal(5, 10, myFunction)
将调用 bestof(5, total, 10, myFunction)
,它本身将调用 5 次 total(10, myFunction)
我正在通过 Mark Lutz 的书 'Learning Python' 学习 python。我有一个关于这个基准测试功能的问题。我了解前两个功能的工作原理,但我不太了解最后一个功能。有人可以向我解释最后一个函数的确切工作原理及其结果 returns 吗?根据作者的说法,它是前两个功能的组合。它 returns 最好的总次数 - 在指定数量的运行中计算调用特定函数 x 次的时间的最少时间。我理解他的解释,但我不理解元组 the bestoftotal 函数 returns。谁能给我解释一下这个元组?
# File timer.py
"""
Homegrown timing tools for function calls.
Does total time, best-of time, and best-of-totals time
"""
import time, sys
try:
timer = time.perf_counter
except AttributeError:
timer = time.clock if sys.platform[:3] == 'win' else time.time
def total(reps, func, *pargs, **kargs):
"""
Total time to run func() reps times.
Returns (total time, last result)
"""
repslist = list(range(reps))
start = timer()
for i in repslist:
ret = func(*pargs, **kargs)
elapsed = timer() - start
return (elapsed, ret)
def bestof(reps, func, *pargs, **kargs):
"""
Quickest func() among reps runs.
Returns (best time, last result)
"""
best = 2 ** 32
for i in range(reps):
start = timer()
ret = func(*pargs, **kargs)
elapsed = timer() - start
if elapsed < best: best = elapsed
return (best, ret)
def bestoftotal(reps1, reps2, func, *pargs, **kargs):
"""
Best of totals:
(best of reps1 run of (total of reps2 run of func))
"""
return bestof(reps1, total, reps2, func, *pargs, **kargs)
bestoftotal
returns 以 total
作为 func 参数调用函数 bestof
。
前两个函数你说你看懂了,为了我自己的理解,我再解释一下。
所有的 *pargs 和 **kargs 都会让事情变得有点混乱。为简化起见,假设我想要 benchark myFunction()
,它不带任何参数。
为了测量 运行 myFunction
10 次所花费的总时间,我这样做:
total(10, myFunction)
为了衡量 5 次试验中最好的(1 次试验 = myFunction
中的 1 运行),我这样做:
bestof(5, myFunction)
现在,我想知道 5 次试验中最好的一次,一次试验由 运行 函数 10 次 ("the best of total trials") 组成。
一试其实就是函数total(10, myFunction)
的一个运行
所以 5 次试验中最好的是 bestof(5, total(10,my Function))
最后一行在 Python 中不是有效的调用。这就是 *pargs 和 **kwargs 有用的地方。
当您调用 bestof(5, total, 10, myFunction)
时,参数 10 和 myFunction
会在调用 total
:
时传递
ret = func(*pargs, **kargs)
行变为 ret = total(10, myFunction)
.
那么 bestoftotal
是做什么的呢?好吧,它需要两个数字 reps1 和 reps 2(在我的例子中是 5 和 10),以及要进行基准测试的函数。它 returns 调用函数 bestof
.
bestoftotal(5, 10, myFunction)
将调用 bestof(5, total, 10, myFunction)
,它本身将调用 5 次 total(10, myFunction)