I felt that it would be possible to measure the execution time of a function just by adding a decorator, so I tried it. (I think it's already in the package)
Since I am using f-string soberly, Python 3.6 or higher is required, but I think that 2.7 will work with some modifications.
Defining a decorator that measures the execution time of a function
import time
def calc_time(message='', parser=None):
def _calc_time(func):
import functools
@functools.wraps(func)
def wrapper(*args, **kargs):
start = time.time()
ret = func(*args, **kargs)
if parser:
parsed_message = parser(message, *args, **kargs)
print(
f'end time : {time.time() - start:0.4} sec : {parsed_message}')
else:
print(f'end time : {time.time() - start:0.4} sec : {message}')
return ret
return wrapper
return _calc_time
I think there are cases where I want to display a message that uses a function argument, so I am making it possible to pass a function object for message processing.
test.py
import time
def message_parser(message, *args, **kargs):
return f"{message} and {args[0]}"
def calc_time(message='', parser=None):
def _calc_time(func):
import functools
@functools.wraps(func)
def wrapper(*args, **kargs):
start = time.time()
ret = func(*args, **kargs)
if parser:
parsed_message = parser(message, *args, **kargs)
print(
f'end time : {time.time() - start:0.4} sec : {parsed_message}')
else:
print(f'end time : {time.time() - start:0.4} sec : {message}')
return ret
return wrapper
return _calc_time
@calc_time()
def test1():
time.sleep(0.2)
print('call test1')
@calc_time('something message')
def test2():
time.sleep(0.2)
print('call test2')
@calc_time('something message', parser=message_parser)
def test3(something_args):
time.sleep(0.2)
print('call test3')
@calc_time('something message',
parser=lambda message, *args, **kargs: f"{message} and {args[0]}")
def test4(something_args):
time.sleep(0.2)
print('call test4')
@calc_time('something message',
parser=lambda message, *args, **kargs: f"{message} and {kargs['something_args']}")
def test5(something_args):
time.sleep(0.2)
print('call test4')
@calc_time(parser=lambda message, *args, **kargs: f"{args[0]}")
def test6(something_args):
time.sleep(0.2)
print('call test4')
def main():
test1()
test2()
test3('something args')
test4('something args')
test5(something_args='something args')
test6('something args')
if __name__ == '__main__':
main()
Execution example
$ python test.py
call test1
end time : 0.2001 sec :
call test2
end time : 0.2 sec : something message
call test3
end time : 0.2001 sec : something message and something args
call test4
end time : 0.2 sec : something message and something args
call test4
end time : 0.2 sec : something message and something args
call test4
end time : 0.2008 sec : something args
About Python Decorators-Qiita [python] Use functools.wrap () with decorator --logging.info (self) [Python: How to make and use various decorators, and the essence | CUBE SUGAR STORAGE](http://momijiame.tumblr.com/post/86112194941/python-%E8%89%B2%E3%80%85%E3% 81% AA% E3% 83% 87% E3% 82% B3% E3% 83% AC% E3% 83% BC% E3% 82% BF% E3% 81% AE% E4% BD% 9C% E3% 82% 8A%E6%96%B9%E3%81%A8%E4%BD%BF%E3%81%84%E6%96%B9%E3%81%9D%E3%81%97%E3%81%A6% E6% 9C% AC% E8% B3% AA)
Recommended Posts