[Tutor] Optimization
Daniel Ehrenberg
littledanehren at yahoo.com
Sat Oct 25 16:10:24 EDT 2003
I just read GvR's essay about optimization in Python
(at http://python.org/doc/essays/list2str.html), and I
came up with a somewhat efficient algorithm for it. It
was too easy to come up with it and it placed second
in efficiency compared to his other programs (he made
7 algorithms for it). The program converts a list of
numerical ASCII values into a string. The best two he
made (mine did better than one, worse than the other)
imported different modules, but mine didn't have to.
There's recently been some debate about programming
styles, so I'm wondering if I did somehting wrong with
that. Here's the best program, written by GvR:
>>> import array
>>> def f7(list):
... return array.array('B', list).tostring()
and here's mine:
>>> f8 = lambda list: ''.join(map(chr, list))
On my computer, his was twice as fast as mine, and
mine was in turn twice as fast as the normally
programmed, most obvious one:
>>> def f1(list):
... string = ""
... for item in list:
... string = string + chr(item)
... return string
My function seemed fairly obvious to me, and it was
better than most of his, so I think there's something
wrong with it that made him not use it. There's
recently been a lot of discussion about which styles
of programming are good and which ones are bad, so is
this a bad style that I used?
Daniel Ehrenberg
__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/
More information about the Tutor
mailing list