python计算运行时间_计算python循环执行时间

我试图对while循环中的while循环计时,它执行所需的总时间,并记录每次循环所需的时间。如果可能的话,我需要一种方法来使用我的代码来实现这一点,或者向我可能还不知道的不同概念开放。

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

31

32

33

34

35

36import random

import time

import sys

def main():

looperCPU = 500

start = time.time()

while (looperCPU != 0):

#start = time.time()

#this is the computation for 3 secs

time.sleep(3)

random_number = random.randint(0,1000)

#Send to printer for processing

#.75 secs to 4.75 secs to generate a pause before printing

secondsPause = random.uniform(.75,4.75)

#This is the printer function

printerLooper = True

while printerLooper == True :

print("Sleeping for", secondsPause," seconds")

print(random_number)

printerLooper = False

#

print("total time taken this loop:", time.time() - start)

looperCPU -= 1

main()

循环将打印一个时间,但我确信它没有考虑嵌套的while循环睡眠时间。我如何允许python在循环时和它需要执行的每个循环时都计时(在本例中是500)?

stackoverflow.com/questions/1557571/…。

@格雷格,如果你读了我的代码,我正在做那个计时模块。我想弄明白的是,这个问题比完整程序的基本时间安排更为深刻。必须为每个循环计时。

贝恩,我的错。

你的嵌套循环实际上不是循环…循环体将始终执行一次。另外,你打印出你要为secondsPause睡觉,但实际上你从来没有这样做过。

当您在初始循环之外设置start时,您保证得到的while循环执行所需的时间不正确。就像是说:

1

2

3

4program_starts = time.time()

while(True):

now = time.time()

print("It has been {0} seconds since the loop started".format(now - program_starts))

这是因为Start在整个执行时间内保持静态,而您的结束时间随着时间的推移而稳步增加。通过在循环中放置开始时间,可以确保开始时间也随着程序的运行而增加。

1

2

3

4

5

6

7

8

9

10while (looperCPU != 0):

start_time = time.time()

# Do some stuff

while printerLooper == True :

print("Sleeping for", secondsPause," seconds")

print(random_number)

printerLooper = False

end_time = time.time()

print("total time taken this loop:", end_time - start_time)

太棒了,这是我需要的。不知怎么的,我的时间安排搞砸了。感谢您提出这个概念。

你可以用这个催产素。使用PIP或CONDA安装。阅读有关pytictoc的文档。它像matlabs tic toc一样工作。基本上只要用tic启动计时器,用toc结束。你可以直接打印或者存储在一个新的变量中。

1

2

3

4

5

6

7

8

9

10

11from pytictoc import TicToc

def main()

t = TicToc() # create instance of class

start_time= t.tic() #start timer

while ():

#end of while loop

end_time = t.toc()

print("end_time", end_time)

请在你的答案中加一点文字。解释一下。代码对任何人都没有帮助。

在python中,您可以查看该文档https://docs.python.org/2/library/timeit.htmlstackoverflow示例:如何使用timeit模块

1

2import timeit

timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

在Jupyter笔记本里,

1

2

3

4

5

6%%timeit -n 100

import pandas as pd

s = pd.Series([101.00, 121.00])

summary = 0

for item in s:

summary+=item

好主意,但是你的定时功能的位置稍微有点偏离,我在这里更正了它:

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

28import random

import time import sys

def main():

looperCPU = 500

start = time.time()

while (looperCPU != 0):

#this is the computation for 3 secs

time.sleep(3)

random_number = random.randint(0,1000)

#Send to printer for processing

#.75 secs to 4.75 secs to generate a pause before printing

secondsPause = random.uniform(.75,4.75)

#This is the printer function

printerLooper = True

myStart = time.time()

while printerLooper == True :

print("Sleeping for", secondsPause," seconds")

print(random_number)

printerLooper = False

print("total time taken this loop:", time.time() - myStart)

looperCPU -= 1

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值