daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # some pre-defined constants
  2. A = 1
  3. B = 2
  4.  
  5. # function that does something critical
  6. def foo(num1, num2):
  7.     # do something
  8.  
  9. # main program.... do something to A and B
  10. for i in range(20):
  11.     # do something to A and B
  12.     # and update A and B during each iteration
  13.  
  14. import timeit
  15. t = timeit.Timer(stmt="foo(num1,num2)")  
  16. print t.timeit(5)
  17.    
  18. import timeit
  19.  
  20. setup = """
  21. A = 1
  22. B = 2
  23.  
  24. def foo(num1, num2):
  25.     pass
  26.  
  27. def mainprog():
  28.     global A,B
  29.     for i in range(20):
  30.         # do something to A and B
  31.         foo(A, B)
  32. """
  33.  
  34. t = timeit.Timer(stmt="mainprog()" setup=setup)
  35. print(t.timeit(5))
  36.    
  37. # some pre-defined constants
  38. A = 1
  39. B = 2
  40.  
  41. # function that does something critical
  42. def foo(n, m):
  43.     pass
  44.  
  45. # main program.... do something to A and B
  46. for i in range(20):
  47.     pass
  48.  
  49. import timeit
  50. t = timeit.Timer(stmt="test.foo(test.A, test.B)", setup="import test")  
  51. print t.timeit(5)
  52.    
  53. def foo(num1, num2):
  54.     def _foo():
  55.         # do something to num1 and num2
  56.         pass
  57.     return _foo
  58.  
  59. A = 1
  60. B = 2
  61.  
  62. import timeit
  63. t = timeit.Timer(foo(A,B))  
  64. print t.timeit(5)
  65.    
  66. def foo(num1, num2):
  67.     # do something to num1 and num2
  68.     pass
  69.  
  70. A = 1
  71. B = 2
  72.  
  73. import timeit, functools
  74. t = timeit.Timer(functools.partial(foo, A, B))
  75. print t.timeit(5)
  76.    
  77. t = timeit.Timer("foo(num1, num2)", "from myfile import foo")
  78. t.timeit(5)
  79.    
  80. setup = """
  81.  # some pre-defined constants
  82. A = 1
  83. B = 2
  84.  
  85. # function that does something critical
  86. def foo(num1, num2):
  87.     # do something
  88.  
  89. # main program.... do something to A and B
  90. for i in range(20):
  91.     # do something to A and B
  92.     # and update A and B during each iteration
  93. """
  94.  
  95. t = timeit.Timer("foo(num1, num2)", setup)
  96. t.timeit(5)
  97.    
  98. def foo(x, y):
  99.     print x*y
  100.  
  101. %prun foo("foo", 100)
  102.    
  103. def f(x,y):
  104.     return x*y
  105.  
  106. v1 = 10
  107. v2 = 20
  108.  
  109. def f_test():
  110.     f(v1,v2)
  111.  
  112. print(timeit.timeit("f_test()", setup="from __main__ import f_test"))
  113.    
  114. def foo(a, b):
  115.     '''Do something to `a` and `b`'''
  116.     return a + b
  117.  
  118. def time_foo():
  119.     '''Create timer object simply without using global variables'''
  120.     import timeit
  121.  
  122.     _foo = foo
  123.     a = 1
  124.     b = 2
  125.  
  126.     # Get `Timer` oject, alternatively just get time with `timeit.timeit()`
  127.     t = timeit.Timer('_foo(a, b)', globals=locals())
  128.  
  129.     return t
  130.    
  131. def foo1(a, b):
  132.     '''Add `a` and `b`'''
  133.     return a + b
  134.  
  135. def foo2(a, b):
  136.     '''More math on `a` and `b`'''
  137.     return (a**2 * b)**2
  138.  
  139. def time_foo(func, **kwargs):
  140.     '''Create timer object simply without using global variables'''
  141.     import timeit
  142.     return timeit.timeit('func(**kwargs)', globals=locals())
  143.  
  144. def run():
  145.     '''Modify inputs to foo and see affect on execution time'''
  146.  
  147.     a = 1
  148.     b = 2
  149.     for i in range(10):
  150.         # Update `a` and `b`
  151.         a += 1
  152.         b += 2
  153.         # Pass args to foo as **kwargs dict
  154.         print('foo1 time: ', time_foo(foo1, **{'a':a, 'b':b}))
  155.         print('foo2 time: ', time_foo(foo2, **{'a':a, 'b':b}))
  156.  
  157.     return None
  158.    
  159. class Data(object):
  160.     """Data Creation"""
  161.     x = [i for i in range(0, 10000)]
  162.     y = tuple([i for i in range(0, 10000)])
  163.     def __init__(self):
  164.         pass
  165.  
  166. import timeit
  167.  
  168. def testIterator(x):
  169.     for i in range(10000):
  170.         z = i
  171.  
  172.  
  173. print timeit.timeit("testIterator(Data.x)", setup="from __main__ import testIterator, Data", number=50)
  174. print timeit.timeit("testIterator(Data.y)", setup="from __main__ import testIterator, Data", number=50)
RAW Paste Data
Top