[Tutor] Aschenputtel problem - Shorter is not better?

Christopher Arndt chris.arndt at web.de
Fri Sep 16 20:29:41 CEST 2005


Pierre Barbier de Reuille schrieb:
> Well, I have some comments ^_^

As should be always expected, when it comes to benchmarking ;-)

> First, about the function 6, it is exactly equivalent (and a little bit
> quicker) to use:
> [...]
> That is, put the try block outside the loop ... however, if this method
> is the quickest it is the only destructive one !

That's ok for my use case. Anyway, you can always copy the original list
beforehand:

list1 = original_list[:]

> Then, you shouldn't time the creation of the list together with
> algorithm. Thus the main loop should be:

It would affect all algorithms in the same way, but, yes, you're probably right.

> Well, I added the creation of a Numeric array ... as the function 8 is
> supposed to work with it.

Hmm, this might be considered cheating, since part of the algorithm is factored
out of the proper function, but if you're already working with Numeric arrays
it wouldn't make a difference.

> In the end, Numeric rocks :) Solution 8 is even faster than solution 6 !

So it seems. For the sake of minimal external requirements I'll stick to
solution 6, though.

You can speed up solution 4 (using sets) further (but not much) for Python 2.4
(but keeping compability with 2.3) if you do this:

try:
    set()
except NameError:
    from sets import Set as set

list1 = [x for x in original_list if condition(x)]
s = set(original_list)
list2 = list(s.difference(list1))

Chris


More information about the Tutor mailing list