[PyKDE] Speed of derived objects

Jim Bublitz jbublitz at nwinternet.com
Tue Nov 26 03:01:01 GMT 2002


On 25-Nov-02 Vincent Wagelaar wrote:
> On Monday 25 November 2002 22:44, Hans-Peter Jansen wrote:
> 
> [snip]
> Hopefully this example is nice enough? I think this is problably
> one of the few cases where you need to make an C++ implementation
> because of speed. It would be nice if calling (overloaded)
> methods in the next version of Python would be speedier!! On my
> computer it's about ten times slower than directly calling
> QListViewItem in both populating the listview and sorting.

Phil might disagree, but I don't think there are major speed
improvements available for the bindings.

You're doing a lot of essentially invariant stuff in the paintCell
and key methods however. Try something like:

from qt import QListViewItem, QColorGroup, QColor

Green  = QColor (0, 155, 0)
Red    = QColor (200, 0, 0)
Orange = QColor (255, 100, 0)

class ColorListItem(QListViewItem):
    def __init__(self, *args):
        QListViewItem.__init__(self, *args)

        # this gives you a QColorGroup with everything
        # set to black, so you probably need to expand
        # this a little - could get tricky if you plan
        # to allow user changes in colors. I didn't see
        # any way to extract the color group info from
        # QListViewItem, but I didn't look very hard
        # either.
        self.colorGroup = QColorGroup ()

        # might not need to do these - I think
        # they're correct, but might not be much
        # faster
        self.ptCell  = QListViewItem.paintCell
        self.keyCall = QListViewItem.key

        try:
            self.pstr = float (str (self.text (0)))
        except:
            self.pstr = 0

        self.keyVal = "%015d" % (self.pstr*100+100000)
        if self.pstr == 0:
            color = Orange
        elif self.pstr > 0:
            color = Green
        else:
            color = Red

        self.colorGroup.setColor (QColorGroup.Text, color)

    def paintCell (self, painter, qg, column, width, align):
        if column == 0:
            self.ptCell (self, painter, self.colorGroup,\
                                    column, width, align)
        else:
            self.ptCell (self, painter, qg, column,\
                                     width, align)

    def key (self, column, asc):
        if column == 0:
            return self.kevVal
        else:
            return self.keyCall(self, column, asc)

I haven't tested this, but the basic idea should give you some
speed improvement - hard to say how much. If the values for the
QListViewItems can be changed, you'll need to break the stuff in
__init__ into a separate method and "re-calculate" when the value
changes (overload setText?). You also probably need a more involved
setup for self.colorGroup (different ctor?), but you should only do
that once. You could also do self.colorGroup as 3 different globals
- one for each condition - but that'll make it harder to customize
colors.


Jim




More information about the PyQt mailing list