0

Trying to make a file that tests how many seconds it takes for python to use a while loop to count how many seconds 1000 loops takes. Not working, I don't know why. Every time I run this it outputs "('It took 0,0')"...The while loop didn't start.

import time
import math

count = 0
PrevTime = time.time()

while count < 1000:
    count +=1
    if count == 1000:
        print ("It took ", time.time() - PrevTime)
        raw_input()
4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by woooee
0

Try to run the loop 1 000 000 times instead of 1000. On my machine, it takes 0.0818829536438 seconds for 1 million times. See also the module timeit.

0

Generally, the structure is more like this.

import time
 
count = 0
PrevTime = time.time()
 
while count < 1000:
    count +=1

print ("It took ", time.time() - PrevTime)
raw_input()

Using "==" in a loop is dicey in more complicated loops, as the counter could be incremented more than once. If you want to do this, use
if count > 999: instead

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.