Palindrome finder: help find ways to speed up?

Dave Brueck dave at pythonapocrypha.com
Wed Apr 9 01:36:59 EDT 2003


On Tue, 8 Apr 2003, Scott David Daniels wrote:

> Dave Brueck wrote:
> > ...
> > def IsPal(s):
> >   r = list(s)
> >   r.reverse()
> >   r = ''.join(r)
> >   ...
>
> This bit should go significantly faster with array:
>
>      from array import array
>
>      def IsPal(s):
>         t = array('c', s)
>         t.reverse()
>         r = t.tostring()

Not necessarily - it has a lot to do with the size of the input string
's'. For short strings (under about 30 chars) the array version is
actually _slower_ than the list one, so you'd definitely want to avoid it
if you're checking for palindromes in single words or small sets of words.
:)

-Dave





More information about the Python-list mailing list