Python memory simulator
I previously mentioned the Python memory simulator project that I was developing, and had set it to run 6 tests on a dual-core machine. 4 of those tests completed, but 2 did not. It turns out that the overhead of performing a sort on Python lists with lengths exceeding 10000, several hundred times per second, eventually makes things slow to a crawl. So, given that I only wanted to compare my memory page objects on a single attribute, I wrote a comparer method and passed it in like so:
systemPages.sort(cmp=my_comparer)
And it did indeed speed up the simulator. Problem is that it would still take about 10 days to complete a given simulation this way, so I dug deeper. Turns out that there is some built in Python magic fairy dust that makes this work faster for whatever reason:
systemPages.sort(key=operator.attrgetter('timesused'))
This makes the sort only pay attention to just the attribute ‘timesused’ when performing a compare. This is also what I thought I was doing when I defined my comparer like so:
def timesused_compare(page1, page2) : return page1.timesused - page2.timesused
*shrug* I don’t understand why one would be faster than the other, but it is. If anyone has insight, please comment.