装饰器法
此方法不改变被装饰函数的返回值
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import functools
import time
def time_me(func):
'''
@summary: cal the time of the fucntion
@param : None
@return: return the res of the func
'''
def wrapper(*args, **kw):
start_time = ()
res = func(*args, **kw)
over_time = ()
print ('{0} run time is {1}'.format(func.__name__, (over_time - start_time).total_seconds()))
return res
return wrapper
@time_me
def test1():
(1)
return 'aaa'
A = test1()
print (A)
()方法:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from datetime import datetime
starttime = ().strftime('%Y-%m-%d %H:%M:%S')
print (starttime)
#long running
#do something other
endtime = ().strftime('%Y-%m-%d %H:%M:%S')
print (endtime)
# 2022-03-21 15:08:06
# 2022-03-21 15:08:06
()获取的是当前时间,在程序执行结束之后,这个方式获得的时间差值为程序执行的时间。
()方法:
start = ()
#long running
#do something other
end = ()
print (end-start)
()获取自纪元以来的当前时间(以秒为单位)。如果系统时钟提供它们,则可能存在秒的分数。所以这个地方返回的是一个浮点型类型。这里获取的也是程序的执行时间。
()方法:
start = ()
#long running
#do something other
end = ()
print end-start
()返回程序开始或第一次被调用clock()以来的CPU时间。 这具有与系统记录一样多的精度。返回的也是一个浮点类型。这里获得的是CPU的执行时间。
注:程序执行时间=cpu时间 + io时间 + 休眠或者等待时间