Equivalent of Perl chomp?

Bengt Richter bokr at oz.net
Mon Feb 4 05:39:06 EST 2002


On Fri, 01 Feb 2002 13:54:51 -0800, Jeff Shannon <jeff at ccvcorp.com> wrote:

>
>
>Bengt Richter wrote:
>
>
>> I think slicing is probably a winner because endswith subsumes more
>> complex slicing options that have to be tested for, and the argument can be
>> multicharacter. I would be surprised if single-character string handling
>> wasn't special-cased a lot for speed, benefiting s[-1:] == '\n'.
>
>Well, one of the things that Tim Peters hinted at is more likely to be the main reason
>that endswith is slower...  every iteration, the endswith version has to resolve a name.
Oops, yup *<8^p

>If you changed the loop to something more like:
>
>func = s.endswith
>for n in xrange(iterations):
>    func('\n')
>
>you would likely see a notable difference in the results.   :)
>
Yup.
>(But I'm too lazy/otherwise busy to actually run the tests myself....)
>
I'm going to continue this in another post. The first trick is
actually timing what you think you're timing ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list