时间循环python

时间:2013-02-07 01:32:13

标签: python time while-loop

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

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在while循环中计时,它需要做的每个循环(在这种情况下为500)?

4 个答案:

答案 0 :(得分:10)

当你在初始循环之外设置start时,你可以保证你获得了执行while循环所需的错误时间。这就好说:

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

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

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)

答案 1 :(得分:1)

正确的想法,但你的计时功能的位置是一点点,我在这里纠正了它:

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()

答案 2 :(得分:1)

在python中,您可以查看此文档https://docs.python.org/2/library/timeit.html
Stackoverflow示例:How to use timeit module

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

在Jupyter笔记本中,

%%timeit -n 100
import pandas as pd
s = pd.Series([101.00, 121.00])
summary = 0
for item in s:
    summary+=item

答案 3 :(得分:1)

您可以使用此pytictoc。使用pip或conda安装。阅读pytictoc上的文档。它像Matlab tic toc一样工作。基本上只用tic开始计时器,用toc结束。您可以直接打印它或将其存储在新变量中。

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)