When Python outruns C++ ...

Anders J. Munch andersjm at inbound.dk
Fri Apr 4 00:17:01 CEST 2003


"Mike Meyer" <mwm at mired.org> wrote:
> "Anders J. Munch" <andersjm at inbound.dk> writes:
> > For a Python example, see the Decorate-Sort-Undecorate pattern.  In
> > C++ the straightforward and fast solution is to use a suitable
> > comparison predicate.  In Python you can do the same, but that's slow,
> > and you are left with a choice between clarity and speed.
>
> The statement about the preferred method in C is simply false. Anytime
> the "suitable comparison predicate" is sufficiently complex - for
> instance, what the unix sort(1) command allows - a DSU that allows
> simple comparisons will be faster than a sort with complex
> comparisons.

The typical case is a simple projection.  And when it's not you can
turn it into one by storing or caching the projected value in the
objects themselves.

> In fact, any time you can simplify the comparison with DSU, the DSU
> sort will always perform faster on n objects for sufficiently large n.

A comparison predicate in C++ (who said anything about C?) that
forwards to instance variables simplifies the comparison similarly.

- Anders







More information about the Python-list mailing list