[Tutor] Re: Optimization

Andrei project5 at redrival.net
Sat Oct 25 16:37:23 EDT 2003


Daniel Ehrenberg wrote on Sat, 25 Oct 2003 13:10:24 -0700 (PDT):

> 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.

That page is pretty old I think. Not sure how old, but it's at least over 2
years, since a translation dated august 2001 is available at
http://home.hanmir.com/~johnsonj/etc/optimization%20anecdote.htm
Things change :). The oldtimers around here should be more capable than I
of judging whether the changes in Python made your solution viable compared
to GvR's.

> 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))

I wouldn't use list (or any other type or built-in) as variable name.

> On my computer, his was twice as fast as mine, and

I like your solution better actually. I don't know the workings of the
array module, but I understand map and lambda. If I had been confronted
with this problem, the array module wouldn't have occured to me.

> 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

Well, there are many ways to do it, and nobody can see all of them :). We
must not rule out the possibility that he took some artistic liberties when
he wrote that piece. Plus he doesn't like lambda and map, which are both
featured in the Python Regrets paper:

http://www.google.com/search?q=cache:2QztD1KncJgJ:www.python.org/doc/essays/ppt/regrets/PythonRegrets.ppt+python+regrets&hl=en&ie=UTF-8

> 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?

Not bad IMO - even though I for one tend to choose list comprehensions
rather than map(). 




More information about the Tutor mailing list