sawyl: (Default)
[personal profile] sawyl
Fed up with the poor performance of one of my python scripts, I decided to spend half an hour or so optimising it, just to see if I couldn't improve things.

Sure enough, when I profiled the base version using a fixed configuration, I found that it was losing most of its time to a series of deepcopy() calls. I changed the internal data structures to use a dictionary instead of a more complex set of arrays and, presto, the run time went down from 7.55s to 1.76s and the function call count went down from 398,359 to 53,678.

But when I re-profiled, I found a couple of other nasties: an N by N array that was being needlessly regenerated for every loop trip and a whole host of calls to has_key(). After adding caching to the main loop to prevent recalculation and replacing all the has_keys() with try ... execpt KeyError ... constructs, I managed to get the time down to 0.97s and a mere 11,236 function calls.

And here's proof of my success: a plot showing how the raw and optimised versions of the program scale as the number of items increases:

So it looks like the big wins were, in order of effectiveness: (a) to use a more efficient algorithm and data structures; (b) to replace has_keys() with exceptions in order to avoid the overhead of the extra subroutine calls and to exploit the (relatively) fast performance of exception handling; (c) to avoid unnecessary work by caching results, trading memory for CPU. Of these, (a) and (c) were obvious, while (b) was only apparent after a little bit of experimentation with the profiler.

Date: 2008-07-16 10:24 am (UTC)
From: [identity profile] leonardo-m.livejournal.com
Generally "in" for dicts is faster than has_key() and it's the most pythonc (has_key is removed from Python3).

Date: 2008-07-16 08:32 pm (UTC)
From: [identity profile] sawyl.livejournal.com
Thanks! I changed my code and, sure enough, it was both faster and clearer than the version using exceptions - a definite win.

I can't say I'll be sorry to see has_key() go. I only ever seem to use it on occasions when I'm forced to switch back and forth between python and perl, so it's probably a cargo cult carry over from perl...

Profile

sawyl: (Default)
sawyl

August 2018

S M T W T F S
   123 4
5 6 7 8910 11
12131415161718
192021222324 25
262728293031 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 5th, 2026 08:16 pm
Powered by Dreamwidth Studios