python 测时间,如何在Python中测量经过时间?

What I want is to start counting time somewhere in my code and then get the passed time, to measure the time it took to execute few function. I think I'm using the timeit module wrong, but the docs are just confusing for me.

import timeit

start = timeit.timeit()

print("hello")

end = timeit.timeit()

print(end - start)

解决方案

If you just want to measure the elapsed wall-clock time between two points, you could use time.time():

import time

start = time.time()

print("hello")

end = time.time()

print(end - start)

This gives the execution time in seconds.

Another option since 3.3 might be to use perf_counter or process_time, depending on your requirements. Before 3.3 it was recommended to use time.clock (thanks Amber). However, it is currently deprecated:

On Unix, return the current processor time as a floating point number

expressed in seconds. The precision, and in fact the very definition

of the meaning of “processor time”, depends on that of the C function

of the same name.

On Windows, this function returns wall-clock seconds elapsed since the

first call to this function, as a floating point number, based on the

Win32 function QueryPerformanceCounter(). The resolution is typically

better than one microsecond.

Deprecated since version 3.3: The behaviour of this function depends

on the platform: use perf_counter() or process_time() instead,

depending on your requirements, to have a well defined behaviour.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值