Skip to content

Instantly share code, notes, and snippets.

@itsyarkee
Created September 4, 2013 02:41
Show Gist options
  • Save itsyarkee/6432211 to your computer and use it in GitHub Desktop.
Save itsyarkee/6432211 to your computer and use it in GitHub Desktop.
A decorator used to calculate the cost time of functions.
#!/usr/bin/env python
#-*-coding:utf-8-*-
"""
A decorator used to calculate the cost time of functions.
"""
import time
from functools import wraps
def func_time(func):
@wraps(func)
def _wrapper(*args, **kwargs):
beg_ts = time.time()
func(*args, **kwargs)
end_ts = time.time()
print("%s cost time: %s" % (func.__name__, end_ts - beg_ts))
return _wrapper
@func_time
def test():
for i in xrange(0, 10000):
t = i * i
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment