Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # speed_testing.py
- "In Python Zen -- There should be one, and preferably only one, obvious way to do it."
- import timeit
- from multiprocessing import Pool
- import time
- COUNT = 100000
- tests = '''
- 0
- 0 #
- pass
- v = t
- a = b = c = 0
- a,b,c = 0,0,0
- a,b,c = '123'
- a,b,c = '1','2','3'
- s = '123': v = 'abc'+s+'xyz
- v = len(t)
- v = int('COUNT')
- v = str(COUNT)
- v = abs(-COUNT)
- v = oct(COUNT)
- v = ord('Z')
- v = chr(90)
- v = sum([6,3,16])
- v = eval('+'.join(map(str,[6,3,16])))
- v = eval('+'.join([str(i) for i in [6,3,16]]))
- v = [j for j in [0] for i in [6,3,16] for j in [j+i]][-1] # inefficient hack
- v = [1,]
- v = (1,)
- v = range(10)
- v = range(COUNT)
- v = (1,)*10
- v = (1,)*COUNT
- V = {i:j for i,j in enumerate(range(10))}
- V = {i:j for i,j in enumerate(range(COUNT))}
- v = {i*10 for i in xrange(10)}
- v = {i:i*2 for i in xrange(10)}
- v = {i*10 for i in xrange(COUNT)}
- v = {i:i*2 for i in xrange(COUNT)}
- v = {i for i in range(10)}
- v = {i for i in range(COUNT)}
- import array; v = array.array('i',range(10))
- import array; v = array.array('i',range(COUNT))
- from collections import deque; v = deque(range(10))
- from collections import deque; v = deque(range(COUNT))
- v = '9'*10
- v = '9'*COUNT
- v = tuple(t)
- v = dict([(1,'abc')])
- v = {1:'abc'}
- v = set(t)
- v = time.time()
- v = time.clock()
- v = list('abc')
- v = map(chr,range(97,123))
- v = map(chr,range(65,91))
- v = divmod(32,9)
- v = (32/9, 32%9)
- v = (32//9, 32%9)
- all([0,1])
- all([1,1])
- all([0,0])
- any([0,1])
- any([1,1])
- any([0,0])
- v = ["a1xb2yc3z"[x::3] for x in 0,1,2]
- v = [1,2,3,4,5]; v.remove(3)
- v = [1,2,3,4,5]; v = [x for x in v if x != 3]
- v = [1,2,3,4,5]; v = [x for x in v if x not in [3]]
- v = [1,2,3,4,5]; v = filter(lambda x: x not in [3], v)
- v = t.index(99999)
- v = [('1' if not i%50 else '0') for i in t]
- v = 3/2
- v = 3//2
- v = math.pow(10,10)
- v = pow(10,10)
- v = 10**10
- v = 10.0**10
- v = 10.0**10.0
- v = 10**2
- v = 2**10
- v = math.sqrt(10)
- v = sqrt(10)
- v = sqrt_lambda(10)
- v = 10*10
- round(123456.789, -2) ###
- t[0] = '+'
- t[COUNT-1] = '+'
- 99999 in t
- 99999 in tSET ###
- 99999 in set(t)
- t.append(COUNT*100)
- v = t.pop()
- v = t.pop(0)
- t.clear() # intended for local usage I presume
- del t[:]
- del t[0]
- v = t[::3]
- v = [i for i in range(len(t)) if not t[i]%3]
- v = t[::100]
- v = [i for i in range(len(t)) if not t[i]%100]
- v = [i for i in range(COUNT)]
- v = [i for i in xrange(COUNT)]
- v = (i for i in xrange(COUNT))
- v = list((i for i in range(COUNT)))
- v = [(i, z) for i, z in enumerate(t)]
- v = [(i, t[i]) for i in range(len(t))]
- v = [(i, t[i]) for i in xrange(len(t))]
- while i < COUNT: i += 1
- while cv.num < COUNT: cv.num += 1
- while L[0] < COUNT: L[0] += 1
- while D[0] < COUNT: D[0] += 1
- for i in xrange(COUNT): pass
- while len(ttt) < COUNT: ttt.append(-1)
- for i in xrange(COUNT): ttt.append(-1)
- for i in range(COUNT): t[i] = -1
- for i in range(COUNT): $$. t[i] = -1
- t = range(100) $$ for i in range(100): $$. t = ['$'+str(i)] + t
- t = range(100) $$ for i in range(100): $$. t += ['$'+str(i)] $$ t.reverse()
- for i in [2,3,4]: iii *= i
- t = [2,3,4]; v = reduce((lambda x, y: x * y), t) # reduced() not imported
- t = [2,3,4]; v = functools.reduce((lambda x, y: x * y), t)
- v = fn_list(COUNT)
- fn() ### same as >>> for i in xrange(COUNT): pass <<< but local
- t.sort()
- v = t[::-1]
- v = list(reversed(t))
- v = sorted(t, reverse=True)
- t.sort(reverse=True)
- v = "-".join(str(i) for i in xrange(10))
- v = "-".join([str(i) for i in xrange(10)])
- v = "-".join(str(i) for i in xrange(COUNT))
- v = "-".join([str(i) for i in xrange(COUNT)])
- v = "-".join(map(str, xrange(COUNT)))
- a,b = 'YES',123; v = 'abc.'+a+'.xyz'
- a,b = 'YES',123; v = 'abc.'+str(b)+'.xyz'
- a,b = 'YES',123; v = 'abc.'+a+str(b)+'.xyz'
- a,b = 'YES',123; v = 'abc.%s.xyz'%a
- a,b = 'YES',123; v = 'abc.%d.xyz'%b
- a,b = 'YES',123; v = 'abc.{}.xyz'.format(a)
- a,b = 'YES',123; v = 'abc.{}.xyz'.format(b)
- a,b = 'YES',123; v = 'abc.{}{}.xyz'.format(a,b)
- a,b = 'YES',123; v = 'abc.{1}{0}.xyz'.format(a,b)
- a,b = 'YES',123; v = 'abc.',str(b),'.xyz'
- v = "$"*COUNT
- v = ["$"]*COUNT
- v = list("$"*COUNT)
- v = ["item1", "item2", "item3"]
- v = "item1 item2 item3".split()
- while i < COUNT: i += 1; s = s + str(i) + '-'
- [ttt.append(i) for i in ta+tb if i not in ttt]
- v = list(set(ta+tb))
- v = list(set(tab))
- v = [i for n, i in enumerate(tab) if i not in tab[:n]]
- v = set(ta+tb)
- v = list(collections.OrderedDict.fromkeys(tab))
- v = list(set(ta) - set(tb)) + list(set(tb) - set(ta))
- v = [x for x in set(ta+tb) if (ta+tb).count(x) < 2]
- v = list(set(ta) ^ set(tb))
- v = set(ta).symmetric_difference(tb)
- v = set(ta).difference(tb) # instead of a - b
- v = set(ta) - set(tb)
- v = set(ta).union(tb) # instead of a | b
- v = set(ta) | set(tb)
- v = set(ta).intersection(tb) # instead of a & b
- v = set(ta) & set(tb)
- v = [(t[i], t[len(t)-(i+1)]) for i in range(len(t))]
- v = [y for x in (t, t[::-1]) for y in x] # ???
- d = collections.deque([1, 2, 3, 4, 5]); d.rotate(-8); v = list(d)
- v = [1, 2, 3, 4, 5]; L = len(v); rotate = lambda t: v[t%L:]+v[:t%L]; v = rotate(-8)
- v = [1, 2, 3, 4, 5]; L = len(v); rotate = lambda t: v[t%L:]+v[:t%L]; v = rotate(8)
- v = [1, 2, 3, 4, 5]; v = [v[t:]+v[:t] for t in v]
- v = t.extend([1, 2, 3, 4, 5])
- def recurse(n): $$. if n == 1: $$.. return 7 $$. else: $$.. return recurse(n-1)*2 + recurse(n-1)*3 $$ recurse(12)
- def recurse(n): $$. if n == 1: $$.. return 7 $$. else: $$.. prev = recurse(n-1) $$.. return prev*2 + prev*3 $$ recurse(12)
- t = range(1000) $$ for i in range(500,1000): x = i in t
- t = dict.fromkeys(range(1000)) $$ for i in range(500,1000): x = i in t
- v = [*t, *t[::-1]] # v3.6+
- v = zip(t, t[::-1])
- a,b = [t,t[::-1]]; v = [(a[i],b[i]) for i in range(len(min(a,b)))] # vs zip
- v = dict(zip(t, t[::-1]))
- v = dict([(k,0) for k in t])
- v = {"key": [0]}; v["key"] += [1,2,3]
- v = {"key": [0]}; v["key"].extend([1,2,3])
- v = {"key": [0]}; [v["key"].append(i) for i in [1,2,3]]
- v = [(t[i],t[-(i-1)]) for i in xrange(len(t))]
- a,b = [t,t[::-1]]; v = [0]*(2*len(min(a,b)));v[::2] = a; v[1::2] = b
- v = []; a,b = [t,t[::-1]]; [v.extend([a[i],b[i]]) for i in range(len(min(a,b)))]
- v = []; map(v.extend,zip(*[t,t[::-1]]))
- v = bytearray(COUNT)
- v = array("B", [0]) * COUNT
- v = array("i", [0]) * COUNT
- v = set(t)
- v = str(t)
- v = tuple(t)
- v = [[] for i in t]
- v = eval(str([[]]*10))
- v = [str(0) for i in t]
- v = {}; [v.update({k:0}) for k in t]
- v = []; [v.append(0) for i in t]
- v = []; v.extend(t)
- v = []; v.__iadd__(t)
- v = (ctypes.c_int * len(t))(*t)
- v = (ctypes.c_int * len(t))(); v[:] = t
- v = (ctypes.c_uint * len(t))(*t)
- v = (ctypes.c_uint * len(t))(); v[:] = t
- v = (ctypes.c_uint32 * len(t))(*t)
- v = (ctypes.c_uint32 * len(t))(); v[:] = t
- v = (ctypes.c_float * len(t))(*t)
- v = (ctypes.c_float * len(t))(); v[:] = t
- v = (ctypes.c_long * len(t))(*t)
- v = (ctypes.c_long * len(t))(); v[:] = t
- v = (ctypes.c_longlong * len(t))(*t)
- v = (ctypes.c_longlong * len(t))(); v[:] = t
- v = (ctypes.c_double * len(t))(*t)
- v = (ctypes.c_double * len(t))(); v[:] = t
- v = (ctypes.c_uint32 * len(t))(*t)
- v = (ctypes.c_uint32 * len(t))(); v[:] = t
- v = (ctypes.c_uint64 * len(t))(*t)
- v = (ctypes.c_uint64 * len(t))(); v[:] = t
- v = (ctypes.c_void_p * len(t))(*t)
- v = (ctypes.c_void_p * len(t))(); v[:] = t
- v = (ctypes.c_uint64 * len(t))(*t)
- v = (ctypes.c_uint64 * len(t))(); v[:] = t
- v = (ctypes.c_uint64 * len(t))(*t)
- v = (ctypes.c_uint64 * len(t))(); v[:] = t
- v = (ctypes.c_uint64 * len(t))(*t)
- v = (ctypes.c_uint64 * len(t))(); v[:] = t
- v = array('d',t)
- v = array('d',t); addr, count = v.buffer_info(); x = ctypes.cast(addr,ctypes.POINTER(ctypes.c_float))
- v = array('d',t); a = (ctypes.c_float * len(v)).from_buffer(v)
- v = array('f',t)
- v = array('f',t); addr, count = v.buffer_info(); x = ctypes.cast(addr,ctypes.POINTER(ctypes.c_float))
- v = array('f',t); a = (ctypes.c_float * len(v)).from_buffer(v)
- v = array('I',t)
- v = array('I',t); addr, count = v.buffer_info(); x = ctypes.cast(addr,ctypes.POINTER(ctypes.c_float))
- v = array('I',t); a = (ctypes.c_float * len(v)).from_buffer(v)
- v = array('I',t); a = (ctypes.c_uint32 * len(v)).from_buffer(v)
- v = array('I',t); a = (ctypes.c_uint64 * len(v)).from_buffer(v) # ValueError: Buffer size too small
- v = array('I',t); assert v.itemsize == 4; addr, count = v.buffer_info(); p = ctypes.cast(addr,ctypes.POINTER(ctypes.c_uint32))
- v = pack('d' * len(t), *t)
- v = pack('f' * len(t), *t)
- v = pack('I' * len(t), *t)
- v = pack('i' * len(t), *t)
- v = pack('L' * len(t), *t)
- v = pack('q' * len(t), *t)
- v = pack('Q' * len(t), *t)
- v = pack('l' * len(t), *t)
- '''.strip().splitlines()
- def fn(n):
- return n
- setup='''
- import ctypes
- from array import array
- from struct import pack
- import itertools
- import functools
- import collections
- import math
- import os
- class Cv(): pass
- cv = Cv()
- cv.num = 0
- i = 0
- iii = 1
- s = ''
- sss = 'testing 1-2-3'
- ttt = []
- L = [0]
- D = {0:0}
- d = {i: i * 3 for i in range(COUNT)}
- t = [i for i in range(COUNT)]
- def sqrt(i):
- return i*i
- sqrt_lambda = lambda i: i*i
- def fn():
- for i in xrange(COUNT):
- pass
- def fn_list(zzz):
- resp = []
- for z in xrange(zzz):
- resp.append(z)
- return resp
- ta = [4,2,3,5,6,1]; tb = [5,7,4,8,9,6]
- tab = ta+tb
- tSET = set(t)
- def f1(x,y):
- return x,y
- def f2(x,y):
- return x,y
- def f3(x,y):
- return x,y
- '''.replace('COUNT',str(COUNT))
- ##### covert4speedtesting.py
- zzz = '''
- '''
- if zzz.strip():
- for z in ('\n\t\t $$..','\n\t $$.','\n $$'):
- a,b = z.split(' ')
- zzz = zzz.replace(a,' %s '%b)
- zzz = zzz.replace(' $$ $$ ','\n\n')
- zzz = zzz.splitlines()
- ppp = []
- for p in zzz:
- for i in [0,-1]:
- try:
- while p[i] in ' $':
- p = (p[1:],p[:-1])[i]
- except:
- break
- ppp += [p]
- zzz = '\n'.join(ppp)
- print zzz
- #####
- if __name__ == '__main__':
- if not zzz.strip():
- '''
- '''
- c = 12 # from start of list line
- for test in tests:
- test = test.replace('COUNT',str(COUNT))
- tttt = test
- for z in ('\n\t\t $$..','\n\t $$.','\n $$'):
- a,b = z.split(' ')
- test = test.replace(' '+b,';')
- tttt = tttt.replace(' %s '%b,a)
- ccc = str(c).zfill(4)
- try:
- print '%s: %.7f' % (ccc,timeit.timeit(stmt=tttt,setup=setup,number=10)), test
- except:
- print ccc+':', '.'*9, 'Error:', test
- if c == 0:
- print tttt, '\n'
- print '%s: %.7f' % (ccc,timeit.timeit(stmt=tttt,setup=setup,number=10)), test
- print
- c += 1
- for n in [2,4,8,16]:
- lowest = []
- for t in xrange(5):
- start = time.time()
- pool = Pool(processes=n)
- for i in xrange(n):
- pool.apply_async(fn, [COUNT/n])
- pool.close()
- pool.join()
- end = time.time()
- lowest.append(end-start)
- print ' %.3f multiprocessing.Pool(processes=%d) \t# apply_async' % (min(lowest),n)
- print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement