21

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.

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

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Bain
  • 834
  • 6
  • 11
  • 17
  • http://stackoverflow.com/questions/1557571/how-to-get-time-of-a-python-program-execution. – Greg Feb 07 '13 at 01:35
  • @greg If you read my code, I'm doing that timing module. What i am trying to figure out is deeper than the basic timing of full program. It has to be timed for every loop. – Bain Feb 07 '13 at 01:38
  • 2
    Your nested loop isn't actually a loop... the loop body will always be executed exactly once. Also, you print that you're going to sleep for `secondsPause`, but you don't actually ever do it. – Gordon Bailey Feb 07 '13 at 01:41
  • The tqdm package can be used for loops in python. Refer to this [related SO discussion](https://stackoverflow.com/questions/45808140/using-tqdm-progress-bar-in-a-while-loop) – KarthikS Feb 27 '23 at 19:48

4 Answers4

28

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:

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)
Greg
  • 5,422
  • 1
  • 27
  • 32
  • Awesome this is what I needed. Somehow I got my time placement messed up. Thank you for this concept. – Bain Feb 07 '13 at 01:51
3

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

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

In Jupyter notebook,

%%timeit -n 100
import pandas as pd
s = pd.Series([101.00, 121.00])
summary = 0
for item in s:
    summary+=item
Community
  • 1
  • 1
Saurabh
  • 7,525
  • 4
  • 45
  • 46
1

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()
hd1
  • 33,938
  • 5
  • 80
  • 91
1

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.

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)
Farouk Yahaya
  • 43
  • 1
  • 8