2017-04-10 69 views
1

我试图测试一个http请求处理码块需要多长时间我的烧瓶控制器内,这里是我所使用的示例代码:Python:time.time()与time.clock()之间的显着区别?

cancelled = [] 
t0 = time.time() 
t1 = time.clock() 
users = requests.get('https://www.example.com/users/') 
for i in users.json(): 
    user = requests.get('https://www.example.com/user/%s' % i['id]').json() 
    if user['status'] == 'Cancelled': 
     cancelled.append(user) 
t2 = time.clock() 
t3 = time.time() 
print t2 - t1 
print t3 - t0 

这里是输出:

2.712326 
76.424875021 

第二time.time()函数的输出与显示结果所用的实际秒数相匹配,所以我不确定time.clock()的值为什么非常小?

编辑:我的系统是OSX和Python 2.7,我的问题是,如果time.time()反映用户体验/等待的实际时间,为什么time.clock()通常被认为是“更好”?

+1

好time.clock()是处理器的时间,如果你是在UNIX和了time.time( )是自时代以来的秒数 – Jacobr365

+0

Dupliate:http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy – Alex

+1

可能的重复[Python - time.clock()与time.time () - 准确性?](http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy) –

回答

1

请注意,从Python 3.3开始,time.clock现在是deprecated,因为行为与平台有关。文档建议使用time.process_timetime.perf_counter来衡量性能。

否则,我会建议使用timeit模块(特别是因为这可以让你在测试环境的更多控制)