Which is faster?

  • Thread starter Aggelos I. Orfanakos
  • Start date
A

Aggelos I. Orfanakos

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.
 
A

Aldo Cortesi

Thus spake Aggelos I. Orfanakos ([email protected]):
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.

Expecting other people to do something simply because you
couldn't be bothered to do it yourself is not polite... That
said, here are the timings on my system:

python ./timeit.py "'a/b/c/'[:-1]"
1000000 loops, best of 3: 0.511 usec per loop

python ./timeit.py "'a/b/c/'.rstrip('/')"
1000000 loops, best of 3: 1.3 usec per loop


As you can see, this suggests that the list access method is
quicker. This is to be expected, since the two methods don't
do the same thing - rstrip will return a copy of your string
with any number of trailing '/'es removed. If there aren't
any, it will return the string as-is. The string access
method will always chop exactly one character off the end.
Even though the results for your specific input are the
same, rstrip is a more complex, and therefore slower, beast.



Cheers,



Aldo




--
Aldo Cortesi
(e-mail address removed)
http://www.nullcube.com
Off: (02) 9283 1131
Mob: 0419 492 863
 
A

Aggelos I. Orfanakos

Yes, I could do the timing myself. Sorry if this was impolite -- it was
not in my intentions. The main reason I asked was about the reason.
Thanks.
 
T

Terry Reedy

Aggelos I. Orfanakos said:
Any idea which of the following is faster?

'a/b/c/'[:-1]
'a/b/c/'.rstrip('/')

I find the first easier to read and mentally process. Others may have a
different answer. But perhaps you meant with the CPython 2.x
implementation ;-)
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.

For more on the CPython (2.2) reason, consider
def f1(s): return s[:-1] ....
def f2(s): return s.rstrip('/') ....
import dis
dis.dis(f1)
0 SET_LINENO 1

3 SET_LINENO 1
6 LOAD_FAST 0 (s)
9 LOAD_CONST 1 (-1)
12 SLICE+2
13 RETURN_VALUE
14 LOAD_CONST 0 (None)
17 RETURN_VALUE 0 SET_LINENO 1

3 SET_LINENO 1
6 LOAD_FAST 0 (s)
9 LOAD_ATTR 1 (rstrip)
12 LOAD_CONST 1 ('/')
15 CALL_FUNCTION 1
18 RETURN_VALUE
19 LOAD_CONST 0 (None)

The second has a load attribute (via dict lookup) that the first does not.
More important, the second has a generic function call versus a specific
byte-coded slice call. The rstrip will also do a slice after it determines
the endpoint of the slice.

Terry J. Reedy
 
P

Peter Otten

Aggelos said:
Any idea which of the following is faster?

'a/b/c/'[:-1]

or

'a/b/c/'.rstrip('/')

Don't ask for the speed, decide whether you want to transform

"a/b/c" --> "a/b/c"
"a/b/c//" --> "a/b/c"

or

"a/b/c" --> "a/b/"
"a/b/c//" --> "a/b/c/"

That is much more important.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,218
Messages
2,571,124
Members
47,727
Latest member
smavolo

Latest Threads

Top