Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> import timeit
- ... number=3
- ... setup = """
- ... from random import random as rand
- ... import numpy
- ... N=7000000
- ... A=[rand() for _ in range(N)]
- ... B=[rand() for _ in range(N)]
- ... sq1 = lambda x: x*x
- ... def sq2(x):
- ... return x*x
- ... """
- ... stmts = [ 'C=[(a+b)**2 for a,b in zip(A,B)]',
- ... 'C=[(a+b)*(a+b) for a,b in zip(A,B)]',
- ... 'C=[sq1(a+b) for a,b in zip(A,B)]',
- ... 'C=[sq2(a+b) for a,b in zip(A,B)]']
- ... for s in stmts:
- ... print "executing", s, "took", timeit.timeit(stmt=s, setup=setup, number=number), "seconds"
- ...
- ... setup = """
- ... from random import random as rand
- ... import numpy
- ... N=7000000
- ... A=[rand() for _ in range(N)]
- ... B=[rand() for _ in range(N)]
- ... A1 = numpy.array(A)
- ... B1 = numpy.array(B)
- ... """
- ... s = """
- ... S1 = A1+B1; C1 = S1 * S1
- ... """
- ... print "executing", s, "took", timeit.timeit(stmt=s, setup=setup, number=number), "seconds"
- executing C=[(a+b)**2 for a,b in zip(A,B)] took 10.4074020386 seconds
- executing C=[(a+b)*(a+b) for a,b in zip(A,B)] took 7.50627994537 seconds
- executing C=[sq1(a+b) for a,b in zip(A,B)] took 9.10019111633 seconds
- executing C=[sq2(a+b) for a,b in zip(A,B)] took 9.08746981621 seconds
- executing S1 = A1+B1; C1 = S1 * S1 took 0.411483049393 seconds
Advertisement
Add Comment
Please, Sign In to add comment