Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def comp1(a, b):
- if len(a) != len(b):
- return False
- for i, v in enumerate(a):
- if v != b[i]:
- return False
- return True
- def comp2(a, b):
- for l, r in map(None, a, b):
- if l != r:
- return False
- return True
- list(a) == list(b)
- tuple(a) == tuple(b)
- all(x == y for x, y in itertools.izip_longest(a, b))
- import timeit
- tests = (
- '''
- a=b=[5]*100
- ''',
- '''
- a=[5]*100
- b=[5]*3
- ''',
- '''
- a=b=(5,)*100
- ''',
- '''
- a=b="This on is a string" * 5
- ''',
- '''
- import array
- a=b=array.array('B', "This on is a string" * 5)
- '''
- )
- common = '''import itertools
- def comp1(a, b):
- if len(a) != len(b):
- return False
- for i, v in enumerate(a):
- if v != b[i]:
- return False
- return True'''
- for i, setup in enumerate(tests):
- t1 = timeit.Timer("comp1(a, b)", setup + common)
- t2 = timeit.Timer("all(x == y for x, y in itertools.izip_longest(a, b))", setup + common)
- t3 = timeit.Timer("all([x == y for x, y in itertools.izip_longest(a, b)])", setup + common)
- t4 = timeit.Timer("list(a) == list(b)", setup + common)
- t5 = timeit.Timer("tuple(a) == tuple(b)", setup + common)
- print '==test %d==' % i
- print ' comp1: %g' % t1.timeit()
- print ' all gen: %g' % t2.timeit()
- print 'all list: %g' % t3.timeit()
- print ' list: %g' % t4.timeit()
- print ' tuple: %gn' % t5.timeit()
- ==test 0==
- comp1: 27.8089
- all gen: 31.1406
- all list: 29.4887
- list: 3.58438
- tuple: 3.25859
- ==test 1==
- comp1: 0.833313
- all gen: 3.8026
- all list: 33.5288
- list: 1.90453
- tuple: 1.74985
- ==test 2==
- comp1: 30.606
- all gen: 31.4755
- all list: 29.5637
- list: 3.56635
- tuple: 1.60032
- ==test 3==
- comp1: 33.3725
- all gen: 35.3699
- all list: 34.2619
- list: 10.2443
- tuple: 10.1124
- ==test 4==
- comp1: 31.7014
- all gen: 32.0051
- all list: 31.0664
- list: 8.35031
- tuple: 8.16301
- sm = difflib.SequenceMatcher(None, a, b)
- for opcode in sm.get_opcodes():
- print " (%s %d:%d %d:%d)" % opcode
- from itertools import starmap, izip
- from operator import eq
- all(starmap(eq, izip(x,y)))
- from itertools import imap
- from operator import eq
- all(imap(eq, x, y))
- x=range(1000)
- y=range(1000);y[10]=0
- timeit tuple(x)==tuple(y)
- 100000 loops, best of 3: 16.9 us per loop
- timeit all(imap(eq,x,y))
- 100000 loops, best of 3: 2.86 us per loop
- list(a) == list(b)
- def cmpLists(a, b):
- return len(a) == len(b) and (False not in [a[i] == b[i] for i in range(0,len(a)])
- # python 2.6 ≤ x < 3.0
- import operator, itertools as it
- def seq_cmp(seqa, seqb):
- return all(it.starmap(operator.eq, it.izip_longest(seqa, seqb)))
Add Comment
Please, Sign In to add comment