I'm running Python 3.2 on a Windows 7 OS in WingIDE 101(Version 4). The environment doesn't really matter in this case, but I figured I should be specific.
My code is as follows. It is not meant to be optimal, just one way to find a prime number:
def isPrime2(n):
if n == 1:
return False
count = 0
for i in range(2,n+1,2):
if n%i == 0:
count = count + 1
if count > 2:
return False
for i in range(1,n+1,2):
if n%i == 0:
count = count + 1
if count > 2:
return False
if count == 2:
return True
start = time.time()
x = isPrime2(571)
end = time.time()
time_interval = end - start
print("%1.15f"%time_interval)
print(x)
The problem I am having is that the time.time() function doesn't seem to be timing. When I run this program I get
0.000000000000000
True
I also tried this up to 30 digits and all of them remained zero.
There is no way my program is this fast considering that I have multiple For loops.
My question is, why is my function not being timed? Or if it is, why is it so fast when I know it should not be?
On Windows, you want to use time.clock()
instead; time.time()
only has 1/60th-second granularity, while the former gives you microsecond granularity instead.
Or, to keep it cross-platform, use timeit.default_timer()
instead, which will use the correct time
function for your platform:
import timeit
start = timeit.default_timer()