Re[2]: [Tutor] sorting the list

Don Arnold Don Arnold" <darnold02@sprynet.com
Thu Feb 27 07:47:01 2003


----- Original Message -----
From: "antonmuhin at rambler.ru" <antonmuhin@rambler.ru>
To: "Don Arnold" <darnold02@sprynet.com>; <tutor@python.org>
Sent: Thursday, February 27, 2003 2:55 AM
Subject: Re[2]: [Tutor] sorting the list



> Hello Don,

Hi!

<snip my using customized sort funtion>

>
> Another solution that can be a little bit quicker for big lists:
>

<snip sort using DSU pattern>

I'm not used to using the decorate/sort/undecorate pattern, so I thought I'd
see what I had been missing:

import time

def mysort(lhs, rhs):
    if rhs[0] <> lhs[0]:
        return cmp(lhs[0],rhs[0])
    else:
        return cmp(int(lhs[1:]),int(rhs[1:]))

a = [ 'a' + str(i) for i in xrange (100000,-1,-1)]

starttime = time.clock()
a.sort(mysort)
endtime = time.clock()
print 'mysort() - %06f elapsed' % (endtime - starttime)

a = [ 'a' + str(i) for i in xrange (100000,-1,-1)]

starttime = time.clock()
s = [(e[0], int(e[1:])) for e in a]
s.sort()
a = ["%s%s" % e for e in s]
endtime = time.clock()
print 'DSU      - %06f elapsed' % (endtime - starttime)

[---output---]

>>>> mysort() - 1.982278 elapsed
>>>> DSU      - 3.472156 elapsed

It looks like using DSU is actually slower than providing your own sort
method. This agrees with my
intuition that the overhead of creating the decorated list then undecorating
it can be sizable, but we
all know intuition can often be wrong. ; )  Is this really the case, or is
there a problem with my
implementation?

>
> Actually, you may consider another presentation for your data too.
>

Well, it was the OP's data. I was just working with what I was given.

> --
> Best regards,
>  anton                            mailto:antonmuhin@rambler.ru
>

Thanks,
Don