3

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?

Saju
  • 3,201
  • 2
  • 23
  • 28
Steven
  • 185
  • 1
  • 3
  • 9

2 Answers2

7

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()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

time.time() doesn't actually guarantee a resolution of better than 1 second. You'll want to use the timeit module instead, which has a higher resolution.

(EDIT)

Examples of alternative usages for `timeit`:

n = 10000
# Method 1: using default_timer()
start = timeit.default_timer()
for i in range (n):
    x = isPrime2(571)
end = timeit.default_timer()
time_interval = (end - start)/n
print("%1.15f"%time_interval)

# Method 2: using timeit()
print("%1.15f"%(timeit.timeit('isPrime2(571)','from __main__ import isPrime2',number=n)/n))
print(x)

The above code replaces the code from the line start = time.time() in the questioner's code and the two alternative methods produce similar timings. See also How to use timeit correctly.

Community
  • 1
  • 1
Simon
  • 10,679
  • 1
  • 30
  • 44
  • timeit worked when I used timeit.timeit() in place of time.time(). I appreciate such a quick reply. I am curious as to why my times are negative though. I had to change the time_interval to start-end instead. – Steven Jan 27 '13 at 23:34
  • @Steven: You can't use `timeit.timeit()` in place of your `time.time()`. You would either use `timeit.default_timer() as @Martijn Pieters showed or put the call to your function as a parameter of `timeit.timeit()`. I've edited my answer to show the appropriate usage. `timeit.timeit()` just returns the time required to call the default `timeit` setup function a million times so the second call would return a number larger than the first call randomly about half the time. – Simon Jan 28 '13 at 02:50