[Checkins] SVN: z3c.form/trunk/src/z3c/form/browser/widget.py Revert change 103644. The "complicated" algorithm preserves class order, so doctests don't fail.

Vincent Fretin vincent.fretin at gmail.com
Fri Sep 11 09:56:34 EDT 2009


You win, your algorithm is better. :-)

impl.py
-----------------------------------------------
KLASS = 'one two'

def before(klass):
    parts = KLASS.split()+[unicode(klass)]
    seen = {}
    unique = []
    for item in parts:
        if item in seen:
            continue
        seen[item]=1
        unique.append(item)
    var = u' '.join(unique)

def withset(klass):
    parts = KLASS.split() + [unicode(klass)]
    var = u' '.join(frozenset(parts))

def withsetsorted(klass):
    parts = KLASS.split() + [unicode(klass)]
    var = u' '.join(sorted(frozenset(parts)))
-----------------------------------------------

bench.py
-----------------------------------------------
import timeit
t = timeit.Timer("impl.before('three')", "import impl")
print t.repeat(3, 200000)


t = timeit.Timer("impl.withset('three')", "import impl")
print t.repeat(3, 200000)

t = timeit.Timer("impl.withsetsorted('three')", "import impl")
print t.repeat(3, 200000)
-----------------------------------------------

$ python bench.py
[1.0895271301269531, 1.0222499370574951, 1.0226280689239502]
[1.1868860721588135, 1.2030000686645508, 1.1814849376678467]
[1.2451817989349365, 1.2396988868713379, 1.2622041702270508]

Vincent


More information about the checkins mailing list