[Tutor] Optimization
Karl Pflästerer
sigurd at 12move.de
Sat Oct 25 16:53:19 EDT 2003
On 25 Oct 2003, Daniel Ehrenberg <- littledanehren at yahoo.com wrote:
> I just read GvR's essay about optimization in Python
> (at http://python.org/doc/essays/list2str.html), and I
I read it after having read your e-mail. It was interesting.
> came up with a somewhat efficient algorithm for it. It
But GvR came up with nearly the same one.
> 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
Funktion 6 is the same as yours.
,----[ f6; Python Patterns - An Optimization Anecdote ]
| import string
| def f6(list):
| return string.joinfields(map(chr, list), "")
`----
The only difference is he uses the strinfg module. I think the time the
essay was written the `join' method of strings didn't exist. The string
module was the only possibility.
> 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.
The reason is written above. The time the essay had been written you
also would have to import the string module.
> 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))
As I wrote yours f8 is nearly the same as GvR's f6. Furthermore you
cheat a bit; you don't give a name to you function but use just a
lambda. That can be a bit faster. f6 written with the join method of
strings instead of the function from the strings module should behave
like your function.
> 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
I don't know for whom that would be the most obvious one; for me not.
The mapping solution seems more obvious if you know a bit functional
programming.
> 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
No there isn't. In fact he did use it as I showed you above.
Furthermore your solution uses lambda; it is well known that Guido is
not the gretaest friend on earth of lambda.
> of programming are good and which ones are bad, so is
> this a bad style that I used?
IMO not.
*But*: do not use *list* as a parameter. It is a builtin.
If you prefer functional or imperative style or a mixture; that's up to
you. Just use the same style in one programm and don't shadow builtins
unless you know why you do it.
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list