[Python-ideas] [Python-Dev] Optimizing list.sort() by checking type in advance

Guido van Rossum guido at python.org
Tue Oct 11 00:15:20 EDT 2016


On Mon, Oct 10, 2016 at 7:56 PM, Elliot Gorokhovsky
<elliot.gorokhovsky at gmail.com> wrote:
> So here's a simple attempt at taking lots of measurements just using
> time.time() with lists of ints. The results are great, if they are valid
> (which I leave to you to judge); even for lists with just one element, it's
> 16% faster!

But that's suspicious in itself -- since no comparisons are needed to
sort a 1-element list, if it's still faster, there must be something
else you're doing (or not doing) that's affecting the time measured.

I wonder if it's the method lookup that's is slower than the entire
call duration? That explains why s[:1] == 'x' is faster than
s.startswith('x'), for example.

A simple nit on your test code: calling time() twice per iteration
could also affect things. I would just call time() once before and
once after the innermost for-loops. (IIRC timeit tries to compensate
for the cost of the loop itself by measuring an empty loop, but that's
got its own set of problems.)

Anyway, you should ignore me and listen to Tim, so I'll shut up now.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list