The following code is used to calculate the fibonacci number:
# test.py
import time
def fib(n):
a, b = 0, 1
while True:
if n == 0:
return a
if n == 1:
return b
n -= 1
a, b = b, a + b
start = time.time()
result = fib(500000)
print('Time elapsed:', time.time() - start)
When I run the code above both by pypy and python, I get an unexpected result: pypy runs the code slower than python. It's strange!
The following is the screenshot of the comparing result:
To check whether it's related to the different python versions or not, I run the following code again by pypy and python:
# test2.py
import time
start = time.time()
sum = 0
for i in range(10 ** 8):
if i & 1:
sum += i
print('Time elapsed:', time.time() - start)
The result of running is expected as the screenshot shows: pypy runs faster.
My question is: is there anything special in test.py
that makes pypy run slower?
The following is my CPU info:
Caption : Intel64 Family 6 Model 23 Stepping 10
DeviceID : CPU0
Manufacturer : GenuineIntel
MaxClockSpeed : 2801
Name : Intel(R) Core(TM)2 Duo CPU P9700 @ 2.80GHz
SocketDesignation : U2E1