Re: [pygame] y-Sorted RenderGroup
If there's no reason to keep the sprites in a dict (sorry, haven't had
time to look at the code, just going by Frank's comments), then using a
bisect-based approach can be very fast.
Basically, you store the data in a sorted-order list and use the bisect
module to add new items (which maintains sort order). If you really do
need the objects in a dict, you can do the work of pushing them into
both dict and list and deleting them when adding/removing items. The
key is that you store the objects in the list as (sortParam, object), so
that the standard Python sorting and comparison automatically works for
the items.
The (sortParam,object) storage means you need to access objects as
list[item][1] (or wrap that in an accessor method), but compared to
calling a Python function (lambda or not) huge numbers of times (which
is pretty darn slow) to do a sort, it should be much faster.
For the common case (wanting all objects), you can use
map( operator.getitem, sortedlist, [1]*len(sortedlist))
to get all second items (the objects in sorted order) very fast.
Asides:
cmp is a built-in, very fast.
list.sort using a custom function is often much slower than building a
temporary list of (sortValue, object) tuples, using default sort, then
doing the operator.getitem (also a built-in) trick above to get the results.
for changed values, simply remove the sprite, then re-insert with bisect.
There is overhead in finding the entry in the sorted list (use
(Continue reading)