Reputation: 5
I have been trying out a timer and it keeps printing 0 and i don't know exactly how to fix it
import time
x=time.time
y=time.time
z=0
b=2
while b<10000:
print b
b=b+10
z=x-y
print z
I think the reason why i keep getting 0 is because i'm doing x-y but if I just leave it and print x or y it comes up with the totally wrong thing. Can anyone tell me how to make this work, so at the end it prints how long the program has been running for, lets say the while loop takes 20 seconds it should print 20 seconds.
Upvotes: 0
Views: 1013
Reputation: 164
Your code never updates y variable after it has been created on 3 line. Place line "y=time.time()" after your loop.
And actually you don't need a timer, you need programm runtime calculation. Look at - How do you calculate program run time in python?
PS Your example is syntactically incorrect.
Upvotes: 1
Reputation: 8108
Fixing the logic and syntax in your example:
import time
x = time.time()
b = 2
while b<10000:
print b
b = b+10
y = time.time()
z = x-y
print z
Will give the desired output. Even better:
import timeit
start = timeit.default_timer()
b = 2
while b<10000:
print b
b = b+10
stop = timeit.default_timer()
print stop - start
Upvotes: 0