Python test function of time

Time Import 
Import datetime 
# test function of time 
DEF cal_time (Fn): 
    "" "computing performance decorator" "" 
    DEF warpper (* args, ** kwargs): 
        STARTIME the time.time = () 
        F = Fn (* args , ** kwargs) 
        endTime = the time.time () 
        Print ( '% S () Runtime: MS S%'% (Fn .__ name__, 1000 * (endTime - STARTIME))) 
        return F 
    return warpper 
@cal_time 
DEF Test () : 
    Print ( "run time counted -----------") 
    for I in Range (1000000): 
        I = I + 1'd 
IF the __name__ == "__main__": 
    Test ()

  This method is called

Import cal_time cal_time from 
'' ' 
Find: In some of the data elements, the process of finding the same key elements by a certain method, 

    table lookup: Find the specified element from the list 
        Input: list, find the element to be 
        output: element index (typically returns None element is not found or -1) 
    list of built-lookup function: index () 
'' ' 
# sequential search: linear search, starting from the first element of the list, the search order, until it finds an element searched list or the last element 
# time complexity of O (n) n --- cycle length of the list halving 
@cal_time 
DEF linear_search (Li, val): list # Li val element to be searched 
    for ind, v in enumerate (li ): 
        IF == V Val: 
            return IND 
    the else: 
        return None 
# binary search: binary search, li [O: n] from the ordered list starts initial candidate region, by treating the intermediate values with candidate regions lookup value comparison, 
# can be less than half a candidate region 
# Li [1,2,3,4,5,6,7,8,9] 

@cal_time 
DEF binary_search (Li, Val):
    0 = left
    len = right (Li) -. 1 
    the while left <= right: # screening candidate region value 
        MID = (left + right) // 2 
        IF Li [MID] == Val: 
            return MID 
        elif Li [MID]> Val: # to be find the value of the left mid 
            right mid = -. 1 
        the else: # Li [mid] value is less than the mean value with lookup val on the right mid 
            left = mid +. 1 
    the else: 
        return None         


#li = [l, 2,3 , 4,5,6,7,8,9] 
Li = List (Range (100000000)) 
linear_search (Li, 3888) 
binary_search (Li, 3888)
             
            

  

Guess you like

Origin www.cnblogs.com/c-jw/p/12574426.html