/test3/timing1.py

http://gmpy.googlecode.com/ · Python · 50 lines · 41 code · 9 blank · 0 comment · 9 complexity · 49a8125cffe3dab421d15b620b5ce543 MD5 · raw file

  1. import time
  2. import gmpy2 as gmpy
  3. try: sum
  4. except NameError:
  5. def sum(x, z):
  6. for item in x: z += item
  7. return z
  8. def timedsum(n, zero):
  9. start=time.clock()
  10. tot=zero
  11. for i in range(n):
  12. tot+=i
  13. stend=time.clock()
  14. return type(zero), stend-start, tot
  15. def timedsum1(n, zero):
  16. start=time.clock()
  17. tot=sum(range(n), zero)
  18. stend=time.clock()
  19. return type(zero), stend-start, tot
  20. def timedmul(n, one):
  21. start=time.clock()
  22. tot=one
  23. for i in range(n):
  24. tot*=(i+1)
  25. stend=time.clock()
  26. return type(one), stend-start, tot
  27. def test(n=100*1000):
  28. print("Sum of %d items of various types:" % n)
  29. for z in 0, 0.0, gmpy.mpz(0), gmpy.mpf(0):
  30. tip, tim, tot = timedsum(n, z)
  31. print(" %5.3f %.0f %s" % (tim, float(tot), tip))
  32. print("Sum of %d items of various types w/2.3 sum builtin:" % n)
  33. for z in 0, 0.0, gmpy.mpz(0), gmpy.mpf(0):
  34. tip, tim, tot = timedsum1(n, z)
  35. print(" %5.3f %.0f %s" % (tim, float(tot), tip))
  36. print("Mul of %d items of various types:" % (n//5))
  37. for z in 1, 1.0, gmpy.mpz(1), gmpy.mpf(1):
  38. tip, tim, tot = timedmul(n//5, z)
  39. print(" %5.3f %s" % (tim, tip))
  40. if __name__=='__main__':
  41. test()