How Can I Increase the Speed of a Large Number of Date Conversions

Terry Reedy tjreedy at udel.edu
Sat Jun 9 13:26:44 EDT 2007


"vdicarlo" <vdicarlo at gmail.com> wrote in message 
news:1181403947.221140.8910 at i38g2000prf.googlegroups.com...
| Many thanks for the lucid and helpful suggestions. Since my date range
| was only a few years, I used Some Other Guy's suggestion above, which
| the forum is saying will be deleted in five days, to make a dictionary
| of the whole range of dates when the script starts. It was so fast it
| wasn't even worth saving in a file. Made the script a LOT faster. I
| guess two thousand function calls must be faster than 200 million?
| Like maybe a hundred thousand times faster?

Any function called repeatedly with the same input is a candidate for a 
lookup table.  This is a fairly extreme example.

|| I also benefitted from studying the other suggestons. I had actually
| implemented an on the fly dictionary caching scheme for one of my
| other calculations. I don't know why it didn't occur to me to do it
| with the dates, except I think I must be assuming, as a newbie
| Pythonista, that the less I do and the more I let the libraries do the
| better off I will be.
|
| Thanks for putting me straight. As someone I know said to me when I
| told him I wanted to learn Python, "the power of Python is in the
| dictionaries".
|
| Funny how long it's taking me to learn that.

Well, look at how many of us also did not quite see the now obvious answer.

Even Python's list.sort() only fairly recently gained the optional 'key' 
parameter, which implements the decorate-sort-undecorate pattern and which 
often obsoletes the original compare-function parameter because it saves 
time by calculating (and saving) the key only once per item instead of once 
each comparison.

>>> import this
The Zen of Python, by Tim Peters
[snip]
Namespaces are one honking great idea -- let's do more of those!

I include lookup dictionaries in this admonition.

tjr






More information about the Python-list mailing list