I was playing around with big numbers, and wrote the following code:
import time
def ispow2(n):
return not n & n - 1
start = time.clock()
ispow2(2**100000000)
end = time.clock()
print(end - start)
Surprisingly, this outputs 0.016864107385627148
, and insanely short amount of time. However, it actually takes about 8
seconds, not 0.02
.
Why is the time module reporting such a fast time when it clearly takes longer than that to run the code?
According to time
, clock()
is deprecated, so I switched it out for process_time()
. I get near identical results. Same with perf_counter()
.
Note: this is running from IDLE. When I ran from command line, the time seemed accurately reported. Perhaps pythonw.exe has something to do with this, but what?
However, when I add another 0
to the end of 2**10...
, it takes ~7 seconds on command line, but reported 0.1781140373572865
.