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

Recommended Answers

All 3 Replies

Lol, try making 1000 into 100000. It's doing it too quick and rounding down the time.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.