1. 程式人生 > >python兩個關於計算方法執行時間的修飾器

python兩個關於計算方法執行時間的修飾器

import time
#import sys
#def wrapper( func ):
#        start = time.time()
#        func( 50 )
#        end = time.time()
#        print ( "used:", end - start )
#        return func

#def timeit():
#    return wrapper

def exeTime( func ):
    def newFunc( *args, **args2 ):
        t0 = time.time()
        back = func( *args, **args2 )
        t1 = time.time()
        print ( t1 - t0 )
        return back
    return newFunc

@exeTime
def foo( a ):
    num = 0
    for i in range( a ):
        time.sleep( 1 )
        num += i ** 2
    print ( "num:", num )

if __name__ == "__main__" :
    foo( 50 )