计算python循环执行时间

Time a while loop 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
36
import 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)?


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

1
2
3
4
program_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
10
while (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
11
from 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
2
import 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
28
import 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()