/test3/timing2.py

http://gmpy.googlecode.com/ · Python · 29 lines · 23 code · 6 blank · 0 comment · 3 complexity · 0841648c6179b7526b8fb0cb7986e91f MD5 · raw file

  1. import time
  2. import gmpy2 as gmpy
  3. def timedfib(n, zero):
  4. start=time.clock()
  5. a=zero; b=a+1
  6. for i in range(n):
  7. a,b=b,a+b
  8. stend=time.clock()
  9. return type(zero), stend-start, a
  10. def timedfibsp(n, one):
  11. start=time.clock()
  12. result=gmpy.fib(n)
  13. stend=time.clock()
  14. return type(one), stend-start, result
  15. def test(n=100*1000):
  16. print("%dth Fibonacci number of various types:" % n)
  17. for z in 0, gmpy.mpz(0), gmpy.mpf(0):
  18. tip, tim, tot = timedfib(n, z)
  19. print(" %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), tip))
  20. tip, tim, tot = timedfibsp(n, 1)
  21. print(" %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), "gmpy.fib"))
  22. if __name__=='__main__':
  23. test()