2to3 (compiles, not tested)
[tag_parser.git] / tests / performance / dedution_iterators.py
blob4eecb83a0c008fd2705b363a1d0baa74d4419313
1 # This Python file uses the following encoding: utf-8
2 '''
3 Created on May 14, 2011
5 This class was to test the functionality and performance of a deduction
6 iterator which accumulated partial results through tuple addition,
7 against one returned it as a growing deque. their performance was
8 nearly identical.
10 @author: mjacob
11 '''
12 import timeit
14 from itertools import chain, combinations
15 from mjacob.algorithms.deduction_iterator import deductions
17 # 1 2 8 98 3728
18 go = "for ded in deductions(start, A.__getitem__):pass"
19 for n in range(1,8):
20 m = 0#12241244
22 A = dict((i, tuple(g
23 for g in chain(*[combinations(range(n), m)
24 for m in range(n+1)]) # xrange(4)
25 if i not in g))
26 for i in range(n))
28 #A = dict((i, tuple((j,k) for j,k in product(xrange(n+1), xrange(n+1,n+n+2)) if j is not i and k is not i)) for i in xrange(n+n+1) if i != n)
29 #print "\n".join(str(x) for x in sorted(A.items()))
30 #A[n] = ((),)
31 #A[n+n+1] = ((),)
32 x="CBA"
33 for d in deductions(0, A.__getitem__):
34 if len(d) < n: continue
35 #print u"; ".join(u"%s -> %s" % (",".join(str(x[j]) for j in b), x[a]) for a,b in d)
36 m += 1
38 print("%s %s" % (n, m))
39 for d in ("deduction_iterator", "deduction_iterator3"):
40 continue
41 prep = """
42 from algorithms.%s import deductions
43 from itertools import product
44 start=0
45 n=%s
46 A = %s
47 """ % (d, n, A)
48 timer = timeit.Timer(go, prep)
50 print("%s %s (%s): %.2f ms/pass" % (d, n, m, 1000 * timer.timeit(number=1)))