Mon, 05 Mar 2007

speeding up pygtk liststore/treeview

I was running into a problem when populating a pygtk liststore where it would take a rather long amount of time and thus slowed down the app quite a lot. I did some looking into ways of speeding it up and found that the slowdown was a rather expensive sort function I was using (soring by column a, then b, then c, then d). So each time it was adding a row to the liststore it would then run through the sort fucntion to find it's place in the list. So for the liststore I was working with I was adding 379 records and it was taking about 2.5 seconds to populate.

Unfortunately there is no way to disconnect a sort function from a liststore, but you can basicly nullify it by creating a simple sort function that doesn't do anything and tell the list store to use that while you populate, then reconnect the complicated sort function after your done populating so it only needs to run the expensive sort function once instead of 379 times.

sort_func = lambda *args: 0
store.set_sort_func(2, sort_func)
store.clear()
... code to populate store ...
store.set_sort_func(2, self.aisort, (2, 3, 6))


This knocked the time down to 0.12 seconds which makes the app much more usable. I also found you could shave a little more time off by also disconnecting the liststore from the treeview to avoid having to update the treeview when each row is getting updated.

treeview.set_model(None)
... code to populate store ...
treeview.set_model(store)


This got things down to 0.10 seconds which isn't a big improvement but does make a difference. And would account for a bigger differene with more rows of data.

posted at: 18:26 | path: /general | permanent link to this entry


Powered by PyBlosxom | RSS 2.0