daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def comp1(a, b):
  2.     if len(a) != len(b):
  3.         return False
  4.     for i, v in enumerate(a):
  5.         if v != b[i]:
  6.             return False
  7.     return True
  8.    
  9. def comp2(a, b):
  10.     for l, r in map(None, a, b):
  11.         if l != r:
  12.             return False
  13.     return True
  14.    
  15. list(a) == list(b)
  16.    
  17. tuple(a) == tuple(b)
  18.    
  19. all(x == y for x, y in itertools.izip_longest(a, b))
  20.    
  21. import timeit
  22.  
  23. tests = (
  24. '''
  25. a=b=[5]*100
  26. ''',
  27.  
  28. '''
  29. a=[5]*100
  30. b=[5]*3
  31. ''',
  32.  
  33. '''
  34. a=b=(5,)*100
  35. ''',
  36.  
  37. '''
  38. a=b="This on is a string" * 5
  39. ''',
  40.  
  41. '''
  42. import array
  43. a=b=array.array('B', "This on is a string" * 5)
  44. '''
  45. )
  46.  
  47. common = '''import itertools
  48. def comp1(a, b):
  49.     if len(a) != len(b):
  50.         return False
  51.     for i, v in enumerate(a):
  52.         if v != b[i]:
  53.             return False
  54.     return True'''
  55.  
  56. for i, setup in enumerate(tests):
  57.     t1 = timeit.Timer("comp1(a, b)", setup + common)
  58.     t2 = timeit.Timer("all(x == y for x, y in itertools.izip_longest(a, b))", setup + common)
  59.     t3 = timeit.Timer("all([x == y for x, y in itertools.izip_longest(a, b)])", setup + common)
  60.     t4 = timeit.Timer("list(a) == list(b)", setup + common)
  61.     t5 = timeit.Timer("tuple(a) == tuple(b)", setup + common)
  62.  
  63.     print '==test %d==' % i
  64.     print '   comp1: %g' % t1.timeit()
  65.     print ' all gen: %g' % t2.timeit()
  66.     print 'all list: %g' % t3.timeit()
  67.     print '    list: %g' % t4.timeit()
  68.     print '   tuple: %gn' % t5.timeit()
  69.    
  70. ==test 0==
  71.    comp1: 27.8089
  72.  all gen: 31.1406
  73. all list: 29.4887
  74.     list: 3.58438
  75.    tuple: 3.25859
  76.  
  77. ==test 1==
  78.    comp1: 0.833313
  79.  all gen: 3.8026
  80. all list: 33.5288
  81.     list: 1.90453
  82.    tuple: 1.74985
  83.  
  84. ==test 2==
  85.    comp1: 30.606
  86.  all gen: 31.4755
  87. all list: 29.5637
  88.     list: 3.56635
  89.    tuple: 1.60032
  90.  
  91. ==test 3==
  92.    comp1: 33.3725
  93.  all gen: 35.3699
  94. all list: 34.2619
  95.     list: 10.2443
  96.    tuple: 10.1124
  97.  
  98. ==test 4==
  99.    comp1: 31.7014
  100.  all gen: 32.0051
  101. all list: 31.0664
  102.     list: 8.35031
  103.    tuple: 8.16301
  104.    
  105. sm = difflib.SequenceMatcher(None, a, b)
  106.     for opcode in sm.get_opcodes():
  107.         print "    (%s %d:%d %d:%d)" % opcode
  108.    
  109. from itertools import starmap, izip
  110. from operator import eq
  111. all(starmap(eq, izip(x,y)))
  112.    
  113. from itertools import imap
  114. from operator import eq
  115. all(imap(eq, x, y))
  116.    
  117. x=range(1000)
  118. y=range(1000);y[10]=0
  119.  
  120. timeit tuple(x)==tuple(y)
  121. 100000 loops, best of 3: 16.9 us per loop
  122.  
  123. timeit all(imap(eq,x,y))
  124. 100000 loops, best of 3: 2.86 us per loop
  125.    
  126. list(a) == list(b)
  127.    
  128. def cmpLists(a, b):
  129.     return len(a) == len(b) and (False not in [a[i] == b[i] for i in range(0,len(a)])
  130.    
  131. # python 2.6 ≤ x < 3.0
  132. import operator, itertools as it
  133.  
  134. def seq_cmp(seqa, seqb):
  135.     return all(it.starmap(operator.eq, it.izip_longest(seqa, seqb)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top