脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python中list查询及所需时间计算操作示例

Python中list查询及所需时间计算操作示例

2021-03-07 00:21gswcfl Python

这篇文章主要介绍了Python中list查询及所需时间计算操作,涉及Python列表list遍历、查询及时间消耗计算相关操作技巧,需要的朋友可以参考下

本文实例讲述了Pythonlist查询及所需时间计算操作。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*-coding=utf-8 -*-
#! python2
#filename: list_query
#date: 2018-03-25
#author: guosw
import time
def 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_time
def 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 n
if __name__ == "__main__":
  i = xrange(100000)
  subscript = list_query(i,9999)
  print subscript

运行结果:

function name: list_query
time cost: 0.00100016593933
9999

希望本文所述对大家Python程序设计有所帮助。

原文链接:https://blog.csdn.net/gswcfl/article/details/79738435

延伸 · 阅读

精彩推荐