Which is faster?
Tony Meyer
t-meyer at ihug.co.nz
Wed Jan 26 21:52:54 EST 2005
> Any idea which of the following is faster?
>
> 'a/b/c/'[:-1]
>
> or
>
> 'a/b/c/'.rstrip('/')
>
> Thanks in advance.
>
> P.S. I could time it but I thought of trying my luck here
> first, in case someone knows already, and of course the reason.
Timing it is almost no work, though:
>>> import timeit
>>> timeit.Timer("'a/b/c/'[:-1]").timeit()
0.23856551600831949
>>> timeit.Timer("'a/b/c/'.rstrip('/')").timeit()
0.74258655778876914
The obvious reason for the former to be faster (although I haven't checked
to see whether it's true) is because the latter checks for a specific
character, whereas the former just snips it off whatever it is.
=Tony.Meyer
More information about the Python-list
mailing list