python performance measure 01
生活随笔
收集整理的這篇文章主要介紹了
python performance measure 01
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
01.性能驗證
方法01:
##最基礎計時方式來驗證性能
exec_times = 100000 orignal_str = "{:<20}{:10.6} s"def slowest_replace():replace_list = []for i, char in enumerate(orignal_str):c = char if char != " " else "-"replace_list.append(c)return "".join(replace_list)def slow_replace():replace_str = ""for i, char in enumerate(orignal_str):c = char if char != " " else "-"replace_str += creturn replace_strdef fast_replace():return "-".join(orignal_str.split())def fastest_replace():return orignal_str.replace(" ", "-")def _time_analyze_(func):from time import clockstart = clock()for i in range(exec_times):func()finish = clock()print "{:<20}{:10.6} s".format(func.__name__ + ":", finish - start)if __name__ == "__main__":fun = [slowest_replace,slow_replace,fast_replace,fastest_replace]for f in fun:_time_analyze_(f)方法02:
##通過定義類的 __enter__ 和 __exit__ 方法來實現對管理的函數計時
import timeclass Timer(object):def __init__(self, verbose=False):self.verbose = verbosedef __enter__(self):self.start = time.time()return selfdef __exit__(self, *args):self.end = time.time()self.secs = self.end - self.startself.msecs = self.secs * 1000 if self.verbose:print 'elapsed time: %f ms' % self.msecsfun = [slowest_replace,slow_replace,fast_replace,fastest_replace] for f in fun:with Timer() as t:_time_analyze_(f)print "=> foo() spends %s s" % t.secsopenstack0-controller0:~/yaowl/_profile # python demo.py slowest_replace: 0.66 s => foo() spends 0.66304397583 s slow_replace: 0.51 s => foo() spends 0.507592916489 s fast_replace: 0.07 s => foo() spends 0.0681991577148 s fastest_replace: 0.05 s => foo() spends 0.0552458763123 s方法03:
通過裝飾器的方式
import time from functools import wrapsdef timer(function):@wraps(function)def function_timer(*args, **kwargs):t0 = time.time()result = function(*args, **kwargs)t1 = time.time()print ("Total time running %s: %s seconds" %(function.func_name, str(t1-t0)))return resultreturn function_timer @timer def _time_analyze_(func):from time import clockstart = clock()for i in range(exec_times):func()finish = clock()#print "{:<20}{:10.6} s".format(func.__name__ + ":", finish - start)if __name__ == "__main__":fun = [slowest_replace,slow_replace,fast_replace,fastest_replace]for f in fun:_time_analyze_(f)controller0:~/yaowl/_profile # python demo.py Total time running _time_analyze_: 0.684864997864 seconds Total time running _time_analyze_: 0.482245922089 seconds Total time running _time_analyze_: 0.0729959011078 seconds Total time running _time_analyze_: 0.0589439868927 seconds方法04:
##通過python自帶庫timeit 詳細參照 https://docs.python.org/2/library/timeit.html
$ python -m timeit '"-".join(str(n) for n in range(100))' 10000 loops, best of 3: 40.3 usec per loop $ python -m timeit '"-".join([str(n) for n in range(100)])' 10000 loops, best of 3: 33.4 usec per loop $ python -m timeit '"-".join(map(str, range(100)))' 10000 loops, best of 3: 25.2 usec per loop$ python -m timeit 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass' 100000 loops, best of 3: 15.7 usec per loop $ python -m timeit 'if hasattr(str, "__nonzero__"): pass' 100000 loops, best of 3: 4.26 usec per loop$ python -m timeit 'try:' ' int.__nonzero__' 'except AttributeError:' ' pass' 1000000 loops, best of 3: 1.43 usec per loop $ python -m timeit 'if hasattr(int, "__nonzero__"): pass' 100000 loops, best of 3: 2.23 usec per loop?
轉載于:https://www.cnblogs.com/yaoweilei/p/7544283.html
總結
以上是生活随笔為你收集整理的python performance measure 01的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql:视图,触发器,事务,存储过程
- 下一篇: orcale开篇