Equivalent of Perl chomp?

Jeff Shannon jeff at ccvcorp.com
Fri Feb 1 22:54:51 CET 2002


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

(But I'm too lazy/otherwise busy to actually run the tests myself....)

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list