runnig

testing (A+B)^2

Dec 30th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. >>> import timeit
  2. ... number=3
  3. ... setup = """
  4. ... from random import random as rand
  5. ... import numpy
  6. ... N=7000000
  7. ... A=[rand() for _ in range(N)]
  8. ... B=[rand() for _ in range(N)]
  9. ... sq1 = lambda x: x*x
  10. ... def sq2(x):
  11. ...   return x*x
  12. ... """
  13. ... stmts = [ 'C=[(a+b)**2 for a,b in zip(A,B)]',        
  14. ...           'C=[(a+b)*(a+b) for a,b in zip(A,B)]',
  15. ...           'C=[sq1(a+b) for a,b in zip(A,B)]',
  16. ...           'C=[sq2(a+b) for a,b in zip(A,B)]']
  17. ... for s in stmts:
  18. ...   print "executing", s, "took", timeit.timeit(stmt=s, setup=setup, number=number), "seconds"
  19. ...  
  20. ... setup = """
  21. ... from random import random as rand
  22. ... import numpy
  23. ... N=7000000
  24. ... A=[rand() for _ in range(N)]
  25. ... B=[rand() for _ in range(N)]
  26. ... A1 = numpy.array(A)
  27. ... B1 = numpy.array(B)
  28. ... """
  29. ... s = """
  30. ... S1 = A1+B1; C1 = S1 * S1
  31. ... """
  32. ... print "executing", s, "took", timeit.timeit(stmt=s, setup=setup, number=number), "seconds"
  33. executing C=[(a+b)**2 for a,b in zip(A,B)] took 10.4074020386 seconds
  34. executing C=[(a+b)*(a+b) for a,b in zip(A,B)] took 7.50627994537 seconds
  35. executing C=[sq1(a+b) for a,b in zip(A,B)] took 9.10019111633 seconds
  36. executing C=[sq2(a+b) for a,b in zip(A,B)] took 9.08746981621 seconds
  37. executing S1 = A1+B1; C1 = S1 * S1 took 0.411483049393 seconds
Advertisement
Add Comment
Please, Sign In to add comment