Evan Klitzke wrote: > >> I guess that's it. The first one reads more like a textbook example which >> is about where I am at. Is there any speed benefit from the one liner? > > The one line is quite a bit faster: > > [EMAIL PROTECTED] ~ $ python -m timeit 's = "onomatopoeia"; s = > s.join(s[::-1])' > 100000 loops, best of 3: 6.24 usec per loop > > [EMAIL PROTECTED] ~ $ python -m timeit ' >> def rev(x): >> mylist = [] >> for char in x: >> mylist.append(char) >> mylist.reverse() >> return "".join(mylist) >> >> s = "onomatopoeia" >> s = rev(s)' > 100000 loops, best of 3: 9.73 usec per loop
For what it's worth, with python 2.5 on my Macbook: [EMAIL PROTECTED] jloden]$ python -m timeit 's = "onomatopoeia"; s = s.join(s[::-1])' 100000 loops, best of 3: 5.2 usec per loop [EMAIL PROTECTED] jloden]$ python -m timeit ' > def rev(x): > mylist = list(x) > mylist.reverse() > return "".join(mylist) > > s = "onomatopoeia" > s = rev(s)' 100000 loops, best of 3: 3.94 usec per loop Note that in the second version, I changed the code a little bit so that it no longer iterates over every char in the string and instead just calls lis() to convert it to a list of chars in order to call list.reverse() on it. -Jay -- http://mail.python.org/mailman/listinfo/python-list