资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

计时函数python的简单介绍

python怎么对列表操作计时

python对列表计时的方法:

创新互联专注于企业成都营销网站建设、网站重做改版、九原网站定制设计、自适应品牌网站建设、H5高端网站建设商城建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为九原等各大城市提供网站开发制作服务。

使用“import”语句导入time包,在列表操作之前用time.time函数获取当前时间,在列表操作之后,再用time.time获取当前时间,用第二次的时间减去第一次的时间就可以了

示例如下:

执行结果如下:

更多Python知识,请关注:Python自学网!!

python如何实现计时?

用python实现计时器功能,代码如下:

''' Simple Timing Function.

This function prints out a message with the elapsed time from the

previous call. It works with most Python 2.x platforms. The function

uses a simple trick to store a persistent variable (clock) without

using a global variable.

'''

import time

def dur( op=None, clock=[time.time()] ):

if op != None:

duration = time.time() - clock[0]

print '%s finished. Duration %.6f seconds.' % (op, duration)

clock[0] = time.time()

# Example

if __name__ == '__main__':

import array

dur() # Initialise the timing clock

opt1 = array.array('H')

for i in range(1000):

for n in range(1000):

opt1.append(n)

dur('Array from append')

opt2 = array.array('H')

seq = range(1000)

for i in range(1000):

opt2.extend(seq)

dur('Array from list extend')

opt3 = array.array('H')

seq = array.array('H', range(1000))

for i in range(1000):

opt3.extend(seq)

dur('Array from array extend')

# Output:

# Array from append finished. Duration 0.175320 seconds.

# Array from list extend finished. Duration 0.068974 seconds.

# Array from array extend finished. Duration 0.001394 seconds.

用python 计时器怎么做,

用python实现计时器功能,代码如下:

''' Simple Timing Function.

This function prints out a message with the elapsed time from the

previous call. It works with most Python 2.x platforms. The function

uses a simple trick to store a persistent variable (clock) without

using a global variable.

'''

import time

def dur( op=None, clock=[time.time()] ):

if op != None:

duration = time.time() - clock[0]

print '%s finished. Duration %.6f seconds.' % (op, duration)

clock[0] = time.time()

# Example

if __name__ == '__main__':

import array

dur()  # Initialise the timing clock

opt1 = array.array('H')

for i in range(1000):

for n in range(1000):

opt1.append(n)

dur('Array from append')

opt2 = array.array('H')

seq = range(1000)

for i in range(1000):

opt2.extend(seq)

dur('Array from list extend')

opt3 = array.array('H')

seq = array.array('H', range(1000))

for i in range(1000):

opt3.extend(seq)

dur('Array from array extend')

# Output:

# Array from append finished. Duration 0.175320 seconds.

# Array from list extend finished. Duration 0.068974 seconds.

# Array from array extend finished. Duration 0.001394 seconds.


文章题目:计时函数python的简单介绍
链接分享:http://cdkjz.cn/article/doojigd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220