Advertisement
here2share

# speed_testing.py

Nov 22nd, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.55 KB | None | 0 0
  1. # speed_testing.py
  2.  
  3. "In Python Zen -- There should be one, and preferably only one, obvious way to do it."
  4.  
  5. import timeit
  6. from multiprocessing import Pool
  7. import time
  8.  
  9. COUNT = 100000
  10.  
  11. tests = '''
  12. 0
  13. 0 #
  14. pass
  15. v = t
  16. a = b = c = 0
  17. a,b,c = 0,0,0
  18. a,b,c = '123'
  19. a,b,c = '1','2','3'
  20. s = '123': v = 'abc'+s+'xyz
  21. v = len(t)
  22. v = int('COUNT')
  23. v = str(COUNT)
  24. v = abs(-COUNT)
  25. v = oct(COUNT)
  26. v = ord('Z')
  27. v = chr(90)
  28. v = sum([6,3,16])
  29. v = eval('+'.join(map(str,[6,3,16])))
  30. v = eval('+'.join([str(i) for i in [6,3,16]]))
  31. v = [j for j in [0] for i in [6,3,16] for j in [j+i]][-1] # inefficient hack
  32. v = [1,]
  33. v = (1,)
  34. v = range(10)
  35. v = range(COUNT)
  36. v = (1,)*10
  37. v = (1,)*COUNT
  38. V = {i:j for i,j in enumerate(range(10))}
  39. V = {i:j for i,j in enumerate(range(COUNT))}
  40. v = {i*10 for i in xrange(10)}
  41. v = {i:i*2 for i in xrange(10)}
  42. v = {i*10 for i in xrange(COUNT)}
  43. v = {i:i*2 for i in xrange(COUNT)}
  44. v = {i for i in range(10)}
  45. v = {i for i in range(COUNT)}
  46. import array; v = array.array('i',range(10))
  47. import array; v = array.array('i',range(COUNT))
  48. from collections import deque; v = deque(range(10))
  49. from collections import deque; v = deque(range(COUNT))
  50. v = '9'*10
  51. v = '9'*COUNT
  52. v = tuple(t)
  53. v = dict([(1,'abc')])
  54. v = {1:'abc'}
  55. v = set(t)
  56. v = time.time()
  57. v = time.clock()
  58. v = list('abc')
  59. v = map(chr,range(97,123))
  60. v = map(chr,range(65,91))
  61. v = divmod(32,9)
  62. v = (32/9, 32%9)
  63. v = (32//9, 32%9)
  64. all([0,1])
  65. all([1,1])
  66. all([0,0])
  67. any([0,1])
  68. any([1,1])
  69. any([0,0])
  70. v = ["a1xb2yc3z"[x::3] for x in 0,1,2]
  71. v = [1,2,3,4,5]; v.remove(3)
  72. v = [1,2,3,4,5]; v = [x for x in v if x != 3]
  73. v = [1,2,3,4,5]; v = [x for x in v if x not in [3]]
  74. v = [1,2,3,4,5]; v = filter(lambda x: x not in [3], v)
  75. v = t.index(99999)
  76. v = [('1' if not i%50 else '0') for i in t]
  77. v = 3/2
  78. v = 3//2
  79. v = math.pow(10,10)
  80. v = pow(10,10)
  81. v = 10**10
  82. v = 10.0**10
  83. v = 10.0**10.0
  84. v = 10**2
  85. v = 2**10
  86. v = math.sqrt(10)
  87. v = sqrt(10)
  88. v = sqrt_lambda(10)
  89. v = 10*10
  90. round(123456.789, -2) ###
  91. t[0] = '+'
  92. t[COUNT-1] = '+'
  93. 99999 in t
  94. 99999 in tSET ###
  95. 99999 in set(t)
  96. t.append(COUNT*100)
  97. v = t.pop()
  98. v = t.pop(0)
  99. t.clear() # intended for local usage I presume
  100. del t[:]
  101. del t[0]
  102. v = t[::3]
  103. v = [i for i in range(len(t)) if not t[i]%3]
  104. v = t[::100]
  105. v = [i for i in range(len(t)) if not t[i]%100]
  106. v = [i for i in range(COUNT)]
  107. v = [i for i in xrange(COUNT)]
  108. v = (i for i in xrange(COUNT))
  109. v = list((i for i in range(COUNT)))
  110. v = [(i, z) for i, z in enumerate(t)]
  111. v = [(i, t[i]) for i in range(len(t))]
  112. v = [(i, t[i]) for i in xrange(len(t))]
  113. while i < COUNT: i += 1
  114. while cv.num < COUNT: cv.num += 1
  115. while L[0] < COUNT: L[0] += 1
  116. while D[0] < COUNT: D[0] += 1
  117. for i in xrange(COUNT): pass
  118. while len(ttt) < COUNT: ttt.append(-1)
  119. for i in xrange(COUNT): ttt.append(-1)
  120. for i in range(COUNT): t[i] = -1
  121. for i in range(COUNT): $$. t[i] = -1
  122. t = range(100) $$ for i in range(100): $$. t = ['$'+str(i)] + t
  123. t = range(100) $$ for i in range(100): $$. t += ['$'+str(i)] $$ t.reverse()
  124. for i in [2,3,4]: iii *= i
  125. t = [2,3,4]; v = reduce((lambda x, y: x * y), t) # reduced() not imported
  126. t = [2,3,4]; v = functools.reduce((lambda x, y: x * y), t)
  127. v = fn_list(COUNT)
  128. fn() ### same as >>> for i in xrange(COUNT): pass <<< but local
  129. t.sort()
  130. v = t[::-1]
  131. v = list(reversed(t))
  132. v = sorted(t, reverse=True)
  133. t.sort(reverse=True)
  134. v = "-".join(str(i) for i in xrange(10))
  135. v = "-".join([str(i) for i in xrange(10)])
  136. v = "-".join(str(i) for i in xrange(COUNT))
  137. v = "-".join([str(i) for i in xrange(COUNT)])
  138. v = "-".join(map(str, xrange(COUNT)))
  139. a,b = 'YES',123; v = 'abc.'+a+'.xyz'
  140. a,b = 'YES',123; v = 'abc.'+str(b)+'.xyz'
  141. a,b = 'YES',123; v = 'abc.'+a+str(b)+'.xyz'
  142. a,b = 'YES',123; v = 'abc.%s.xyz'%a
  143. a,b = 'YES',123; v = 'abc.%d.xyz'%b
  144. a,b = 'YES',123; v = 'abc.{}.xyz'.format(a)
  145. a,b = 'YES',123; v = 'abc.{}.xyz'.format(b)
  146. a,b = 'YES',123; v = 'abc.{}{}.xyz'.format(a,b)
  147. a,b = 'YES',123; v = 'abc.{1}{0}.xyz'.format(a,b)
  148. a,b = 'YES',123; v = 'abc.',str(b),'.xyz'
  149. v = "$"*COUNT
  150. v = ["$"]*COUNT
  151. v = list("$"*COUNT)
  152. v = ["item1", "item2", "item3"]
  153. v = "item1 item2 item3".split()
  154. while i < COUNT: i += 1; s = s + str(i) + '-'
  155. [ttt.append(i) for i in ta+tb if i not in ttt]
  156. v = list(set(ta+tb))
  157. v = list(set(tab))
  158. v = [i for n, i in enumerate(tab) if i not in tab[:n]]
  159. v = set(ta+tb)
  160. v = list(collections.OrderedDict.fromkeys(tab))
  161. v = list(set(ta) - set(tb)) + list(set(tb) - set(ta))
  162. v = [x for x in set(ta+tb) if (ta+tb).count(x) < 2]
  163. v = list(set(ta) ^ set(tb))
  164. v = set(ta).symmetric_difference(tb)
  165. v = set(ta).difference(tb)             # instead of a - b
  166. v = set(ta) - set(tb)
  167. v = set(ta).union(tb)                  # instead of a | b
  168. v = set(ta) | set(tb)
  169. v = set(ta).intersection(tb)           # instead of a & b
  170. v = set(ta) & set(tb)
  171. v = [(t[i], t[len(t)-(i+1)]) for i in range(len(t))]
  172. v = [y for x in (t, t[::-1]) for y in x] # ???
  173. d = collections.deque([1, 2, 3, 4, 5]); d.rotate(-8); v = list(d)
  174. v = [1, 2, 3, 4, 5]; L = len(v); rotate = lambda t: v[t%L:]+v[:t%L]; v = rotate(-8)
  175. v = [1, 2, 3, 4, 5]; L = len(v); rotate = lambda t: v[t%L:]+v[:t%L]; v = rotate(8)
  176. v = [1, 2, 3, 4, 5]; v = [v[t:]+v[:t] for t in v]
  177. v = t.extend([1, 2, 3, 4, 5])
  178. def recurse(n): $$. if n == 1: $$.. return 7 $$. else: $$.. return recurse(n-1)*2 + recurse(n-1)*3 $$ recurse(12)
  179. def recurse(n): $$. if n == 1: $$.. return 7 $$. else: $$.. prev = recurse(n-1) $$.. return prev*2 + prev*3 $$ recurse(12)
  180. t = range(1000) $$ for i in range(500,1000): x = i in t
  181. t = dict.fromkeys(range(1000)) $$ for i in range(500,1000): x = i in t
  182. v = [*t, *t[::-1]] # v3.6+
  183. v = zip(t, t[::-1])
  184. a,b = [t,t[::-1]]; v = [(a[i],b[i]) for i in range(len(min(a,b)))] # vs zip
  185. v = dict(zip(t, t[::-1]))
  186. v = dict([(k,0) for k in t])
  187. v = {"key": [0]}; v["key"] += [1,2,3]
  188. v = {"key": [0]}; v["key"].extend([1,2,3])
  189. v = {"key": [0]}; [v["key"].append(i) for i in [1,2,3]]
  190. v = [(t[i],t[-(i-1)]) for i in xrange(len(t))]
  191. a,b = [t,t[::-1]]; v = [0]*(2*len(min(a,b)));v[::2] = a; v[1::2] = b
  192. v = []; a,b = [t,t[::-1]]; [v.extend([a[i],b[i]]) for i in range(len(min(a,b)))]
  193. v = []; map(v.extend,zip(*[t,t[::-1]]))
  194. v = bytearray(COUNT)
  195. v = array("B", [0]) * COUNT
  196. v = array("i", [0]) * COUNT
  197. v = set(t)
  198. v = str(t)
  199. v = tuple(t)
  200. v = [[] for i in t]
  201. v = eval(str([[]]*10))
  202. v = [str(0) for i in t]
  203. v = {}; [v.update({k:0}) for k in t]
  204. v = []; [v.append(0) for i in t]
  205. v = []; v.extend(t)
  206. v = []; v.__iadd__(t)
  207. v = (ctypes.c_int * len(t))(*t)
  208. v = (ctypes.c_int * len(t))(); v[:] = t
  209. v = (ctypes.c_uint * len(t))(*t)
  210. v = (ctypes.c_uint * len(t))(); v[:] = t
  211. v = (ctypes.c_uint32 * len(t))(*t)
  212. v = (ctypes.c_uint32 * len(t))(); v[:] = t
  213. v = (ctypes.c_float * len(t))(*t)
  214. v = (ctypes.c_float * len(t))(); v[:] = t
  215. v = (ctypes.c_long * len(t))(*t)
  216. v = (ctypes.c_long * len(t))(); v[:] = t
  217. v = (ctypes.c_longlong * len(t))(*t)
  218. v = (ctypes.c_longlong * len(t))(); v[:] = t
  219. v = (ctypes.c_double * len(t))(*t)
  220. v = (ctypes.c_double * len(t))(); v[:] = t
  221. v = (ctypes.c_uint32 * len(t))(*t)
  222. v = (ctypes.c_uint32 * len(t))(); v[:] = t
  223. v = (ctypes.c_uint64 * len(t))(*t)
  224. v = (ctypes.c_uint64 * len(t))(); v[:] = t
  225. v = (ctypes.c_void_p * len(t))(*t)
  226. v = (ctypes.c_void_p * len(t))(); v[:] = t
  227. v = (ctypes.c_uint64 * len(t))(*t)
  228. v = (ctypes.c_uint64 * len(t))(); v[:] = t
  229. v = (ctypes.c_uint64 * len(t))(*t)
  230. v = (ctypes.c_uint64 * len(t))(); v[:] = t
  231. v = (ctypes.c_uint64 * len(t))(*t)
  232. v = (ctypes.c_uint64 * len(t))(); v[:] = t
  233. v = array('d',t)
  234. v = array('d',t); addr, count = v.buffer_info(); x = ctypes.cast(addr,ctypes.POINTER(ctypes.c_float))
  235. v = array('d',t); a = (ctypes.c_float * len(v)).from_buffer(v)
  236. v = array('f',t)
  237. v = array('f',t); addr, count = v.buffer_info(); x = ctypes.cast(addr,ctypes.POINTER(ctypes.c_float))
  238. v = array('f',t); a = (ctypes.c_float * len(v)).from_buffer(v)
  239. v = array('I',t)
  240. v = array('I',t); addr, count = v.buffer_info(); x = ctypes.cast(addr,ctypes.POINTER(ctypes.c_float))
  241. v = array('I',t); a = (ctypes.c_float * len(v)).from_buffer(v)
  242. v = array('I',t); a = (ctypes.c_uint32 * len(v)).from_buffer(v)
  243. v = array('I',t); a = (ctypes.c_uint64 * len(v)).from_buffer(v) # ValueError: Buffer size too small
  244. v = array('I',t); assert v.itemsize == 4; addr, count = v.buffer_info(); p = ctypes.cast(addr,ctypes.POINTER(ctypes.c_uint32))
  245. v = pack('d' * len(t), *t)
  246. v = pack('f' * len(t), *t)
  247. v = pack('I' * len(t), *t)
  248. v = pack('i' * len(t), *t)
  249. v = pack('L' * len(t), *t)
  250. v = pack('q' * len(t), *t)
  251. v = pack('Q' * len(t), *t)
  252. v = pack('l' * len(t), *t)
  253. '''.strip().splitlines()
  254.  
  255. def fn(n):
  256.     return n
  257.  
  258. setup='''
  259. import ctypes
  260. from array import array
  261. from struct import pack
  262. import itertools
  263. import functools
  264. import collections
  265. import math
  266. import os
  267. class Cv(): pass
  268. cv = Cv()
  269. cv.num = 0
  270. i = 0
  271. iii = 1
  272. s = ''
  273. sss = 'testing 1-2-3'
  274. ttt = []
  275. L = [0]
  276. D = {0:0}
  277. d = {i: i * 3 for i in range(COUNT)}
  278. t = [i for i in range(COUNT)]
  279. def sqrt(i):
  280.     return i*i
  281. sqrt_lambda = lambda i: i*i
  282. def fn():
  283.     for i in xrange(COUNT):
  284.         pass
  285. def fn_list(zzz):
  286.     resp = []
  287.     for z in xrange(zzz):
  288.         resp.append(z)
  289.     return resp
  290. ta = [4,2,3,5,6,1]; tb = [5,7,4,8,9,6]
  291. tab = ta+tb
  292. tSET = set(t)
  293. def f1(x,y):
  294.     return x,y
  295. def f2(x,y):
  296.     return x,y
  297. def f3(x,y):
  298.     return x,y
  299. '''.replace('COUNT',str(COUNT))
  300.  
  301. ##### covert4speedtesting.py
  302.  
  303. zzz = '''
  304.  
  305. '''
  306.  
  307. if zzz.strip():
  308.     for z in ('\n\t\t $$..','\n\t $$.','\n $$'):
  309.         a,b = z.split(' ')
  310.         zzz = zzz.replace(a,' %s '%b)
  311.     zzz = zzz.replace(' $$  $$ ','\n\n')
  312.     zzz = zzz.splitlines()
  313.     ppp = []
  314.     for p in zzz:
  315.         for i in [0,-1]:
  316.             try:
  317.                 while p[i] in ' $':
  318.                     p = (p[1:],p[:-1])[i]
  319.             except:
  320.                 break
  321.         ppp += [p]
  322.     zzz = '\n'.join(ppp)
  323.     print zzz
  324.  
  325. #####
  326.  
  327. if __name__ == '__main__':
  328.     if not zzz.strip():
  329.         '''
  330.         '''
  331.         c = 12 # from start of list line
  332.         for test in tests:
  333.             test = test.replace('COUNT',str(COUNT))
  334.             tttt = test
  335.            
  336.             for z in ('\n\t\t $$..','\n\t $$.','\n $$'):
  337.                 a,b = z.split(' ')
  338.                 test = test.replace(' '+b,';')
  339.                 tttt = tttt.replace(' %s '%b,a)
  340.            
  341.             ccc = str(c).zfill(4)
  342.             try:
  343.                 print '%s: %.7f' % (ccc,timeit.timeit(stmt=tttt,setup=setup,number=10)), test
  344.             except:
  345.                 print ccc+':', '.'*9, 'Error:', test
  346.                 if c == 0:
  347.                     print tttt, '\n'
  348.                     print '%s: %.7f' % (ccc,timeit.timeit(stmt=tttt,setup=setup,number=10)), test
  349.             print
  350.             c += 1
  351.  
  352.         for n in [2,4,8,16]:
  353.             lowest = []
  354.             for t in xrange(5):
  355.                 start = time.time()
  356.                 pool = Pool(processes=n)
  357.                 for i in xrange(n):
  358.                     pool.apply_async(fn, [COUNT/n])
  359.                 pool.close()
  360.                 pool.join()
  361.                 end = time.time()
  362.                 lowest.append(end-start)
  363.             print ' %.3f multiprocessing.Pool(processes=%d) \t# apply_async' % (min(lowest),n)
  364.             print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement