Equivalent of Perl chomp?
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):
you would likely see a notable difference in the results. :)
(But I'm too lazy/otherwise busy to actually run the tests myself....)
More information about the Python-list