Python速度testing – 时差 – 毫秒

为了加速testing一段代码,在Python中比较两次的正确方法是什么? 我试着阅读API文档。 我不确定我了解timedelta的事情。

到目前为止,我有这样的代码:

from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = datetime.now() print t2 # what am I missing? # I'd like to print the time diff here 

datetime.timedelta只是两个date时间之间的差异…所以这就像一段时间,以天/秒/微秒

 >>> a = datetime.datetime.now() >>> b = datetime.datetime.now() >>> c = b - a >>> c datetime.timedelta(0, 4, 316543) >>> c.days 0 >>> c.seconds 4 >>> c.microseconds 316543 

请注意, c.microseconds只返回c.microseconds的微秒部分! 为了时间目的,总是使用c.total_seconds()

你可以用datetime.timedelta做各种math,例如:

 >>> c / 10 datetime.timedelta(0, 0, 431654) 

看看CPU时间而不是挂钟时间可能更有用,尽pipe…这是依赖于操作系统的,但是在类Unix系统下,请查看“time”命令。

由于Python 2.7有timedelta.total_seconds()方法。 所以,要获得经过的毫秒:

 >>> import datetime >>> a = datetime.datetime.now() >>> b = datetime.datetime.now() >>> delta = b - a >>> print delta 0:00:05.077263 >>> int(delta.total_seconds() * 1000) # milliseconds 5077 

您可能想要使用timeit模块 。

你也可以使用:

 import time start = time.clock() do_something() end = time.clock() print "%.2gs" % (end-start) 

或者你可以使用python分析器 。

我知道这是迟到,但我真的很喜欢使用:

 start = time.time() ##### your timed code here ... ##### print "Process time: " + (time.time() - start) 

time.time()给出了自纪元以来的秒数。 由于这是以秒为单位的标准化时间,因此您可以简单地从结束时间中减去开始时间以获取处理时间(以秒为单位)。 time.clock()对基准testing非常time.clock() ,但是如果你想知道你的过程花费了多长时间,那么我发现它是无用的。 例如,说“我的过程需要10秒钟”比直接说“我的过程需要10个处理器时钟单元”要直观得多。

 >>> start = time.time(); sum([each**8.3 for each in range(1,100000)]) ; print (time.time() - start) 3.4001404476250935e+45 0.0637760162354 >>> start = time.clock(); sum([each**8.3 for each in range(1,100000)]) ; print (time.clock() - start) 3.4001404476250935e+45 0.05 

在上面的第一个例子中,time.clock()的时间为0.05,time.time()的时间为0.06377

 >>> start = time.clock(); time.sleep(1) ; print "process time: " + (time.clock() - start) process time: 0.0 >>> start = time.time(); time.sleep(1) ; print "process time: " + (time.time() - start) process time: 1.00111794472 

在第二个例子中,即使进程睡了一秒,处理器时间也会显示“0”。 time.time()正确显示了一秒多一点。

你可以简单地打印这个区别:

 print tend - tstart 

下面的代码应该显示时间detla …

 from datetime import datetime tstart = datetime.now() # code to speed test tend = datetime.now() print tend - tstart 

time.time()/ datetime适合快速使用,但并不总是100%准确。 出于这个原因,我喜欢使用一个std lib 分析器 (特别是hotshot)来找出什么是什么。

您可能想要查看configuration文件模块。 你会更好地读出你放慢的速度,你的大部分工作都会自动完成。

这是一个自定义函数,它模仿了Matlab的/ Octave的tic toc函数。

使用示例:

 time_var = time_me(); # get a variable with the current timestamp ... run operation ... time_me(time_var); # print the time difference (eg '5 seconds 821.12314 ms') 

function:

 def time_me(*arg): if len(arg) != 0: elapsedTime = time.time() - arg[0]; #print(elapsedTime); hours = math.floor(elapsedTime / (60*60)) elapsedTime = elapsedTime - hours * (60*60); minutes = math.floor(elapsedTime / 60) elapsedTime = elapsedTime - minutes * (60); seconds = math.floor(elapsedTime); elapsedTime = elapsedTime - seconds; ms = elapsedTime * 1000; if(hours != 0): print ("%d hours %d minutes %d seconds" % (hours, minutes, seconds)) elif(minutes != 0): print ("%d minutes %d seconds" % (minutes, seconds)) else : print ("%d seconds %f ms" % (seconds, ms)) else: #print ('does not exist. here you go.'); return time.time() 

我不是一个Python程序员,但是我知道如何使用Google ,下面是我发现的:使用“ – ”运算符。 要完成你的代码:

 from datetime import datetime tstart = datetime.now() # code to speed test tend = datetime.now() print tend - tstart 

此外,它看起来像你可以使用strftime()函数格式化时间跨度计算,以便呈现时间,但让你快乐。

您可以像这样使用timeit来testing名为module.py的脚本

 $ python -mtimeit -s 'import module'