时间循环python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14742089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:17:24  来源:igfitidea点击:

Time a while loop python

pythontimewhile-loop

提问by Bain

I'm trying to time a while loop within a while loop, total time it takes to execute, and record the time it takes to do so, every time it loops. I need a way to achieve this using my code if possible, or open to different concepts I may not know of yet.

我试图在 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()

The loop will print a time, but I'm very certain it is not taking into account the nested while loop sleep time. How can I allow python to time both while loops, every loop that it will need to do (in this case 500)?

循环会打印一个时间,但我很确定它没有考虑嵌套的 while 循环睡眠时间。我怎样才能让 python 计时两个while循环,它需要做的每个循环(在这种情况下是500)?

采纳答案by Greg

When you set start outside your initial loop you are guaranteeing that you are getting the incorrect time it takes for the while loop to execute. It would be like saying:

当您在初始循环之外设置 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))

This is because start stays static for the entire execution time while your end time is steadily increasing with the passing of time. By placing start time within the loop you ensure that your start time is also increasing as the program runs.

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

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)

回答by hd1

Right idea, but your placement of the timing functions is a wee bit off, I've corrected it here:

想法是对的,但是您对计时功能的放置有点偏离,我在这里更正了它:

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

回答by Saurabh

In python you may look into this doc https://docs.python.org/2/library/timeit.html
Stackoverflow example: How to use timeit module

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

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

In Jupyter notebook,

在 Jupyter 笔记本中,

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

回答by Farouk Yahaya

You can use this pytictoc. install using pip or conda. read documentation on pytictoc. It works like matlabs tic toc. basically just start timer with tic and end with toc. and u can print it directly or store it in a new variable.

你可以使用这个pytictoc。使用 pip 或 conda 安装。阅读有关pytictoc 的文档。它的工作原理类似于 matlabs 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)