本文實例講述了Python中list查詢及所需時間計算操作。分享給大家供大家參考,具體如下:
# -*-coding=utf-8 -*-#! python2#filename: list_query#date: 2018-03-25#author: guoswimport timedef cost_time(fun): def cost(*args,**kwargs): stime = time.time() x = fun(*args,**kwargs) etime = time.time() print "function name:",fun.__name__ print "time cost:",etime-stime return x return cost@cost_timedef list_query(_list,_val): low = 0 height = len(i) - 1 mid = (height - low) // 2 for n in xrange(height+1): if _list[n] == _val: return n for n in xrange(mid,height+1): if _list[n] == _val: return nif __name__ == "__main__": i = xrange(100000) subscript = list_query(i,9999) print subscript
運行結果:
function name: list_query
time cost: 0.00100016593933
9999
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數組操作技巧總結》、《Python字符串操作技巧匯總》、《Python函數使用技巧總結》、《Python入門與進階經典教程》及《Python數據結構與算法教程》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答