1. 程式人生 > >Python cProfile 效能測試裝飾器方法

Python cProfile 效能測試裝飾器方法

指令碼執行效能測試

	import cProfile
	from cProfile import Profile
	def profile_wrapper(func):
	    def wrapper(*args, **kwargs):
	        prof = Profile()
	        prof.enable()
	        func(*args, **kwargs)
	        prof.create_stats()
	        prof.print_stats()
	
	    return wrapper

	@profile_wrapper
	def test():
	 #
	 	cProfile.run('tv()')
	 	tv_2()
	 
 def tv():
      return 8
 def tv_2():
	 return 8