Created
September 4, 2013 02:41
-
-
Save itsyarkee/6432211 to your computer and use it in GitHub Desktop.
A decorator used to calculate the cost time of functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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