【发布时间】:2014-10-22 15:37:10
【问题描述】:
我在使用 time 或 timeit 函数来确定 python 中两种算法的运行时间时遇到了一些麻烦。到目前为止我有这个
def normal(sound):
for s in getSamples(sound):
largest=max(0,getSampleValue(s))
amplification = 32767.0/largest
for s in getSamples(sound):
louder = amplification*getSampleValue(s)
setSampleValue(s,louder)
def onlyMax(sound):
for s in getSamples(sound):
value=getSampleValue(s)
if value>0:
setSampleValue(s,32767)
if value<=0:
setSampleValue(s,-32768)
import time
def timetwoalgorithms(sound):
print time.time("normal(sound)","onlyMax(sound)")
程序应该测量运行每个函数所需的时间,然后输出/打印每个程序的运行时间。
【问题讨论】:
-
我不确定我是否完全理解 timeit 或 time 函数,但我们将不胜感激,但似乎无法在堆栈中找到答案
-
我写了一个答案here
标签: algorithm python-2.7 audio time timeit