473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Which is faster?

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.

Jul 18 '05 #1
4 1575
Thus spake Aggelos I. Orfanakos (ao********@gmail.com):
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
al**@nullcube.com
http://www.nullcube.com
Off: (02) 9283 1131
Mob: 0419 492 863
Jul 18 '05 #2
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.

Jul 18 '05 #3

"Aggelos I. Orfanakos" <ao********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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 dis.dis(f2)

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


Jul 18 '05 #4
Aggelos I. Orfanakos wrote:
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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: MLH | last post by:
I have a table I can open as table type recordset or a dynaset. Searching for a particular value in the table's main keyfield, which would be faster and less strain on the application......
18
by: junky_fellow | last post by:
which of the following is faster and why ? if ( x == 1 ) { } or if ( x != 0 ) {
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
7
by: Sunil Varma | last post by:
Is accessing function by it's name faster or accessing it by its address faster?
4
by: Sonnich | last post by:
Hi I have a costum function for a special search, which sort strings. This is currently the place where I can save a lot of time (~70%) if possible. So, which is faster: for($j =...
25
by: Ganesh | last post by:
Hi, This is a question that pertains to pointers in general (C or C++). Which of the following is faster and why? for (int i = 0; i < N; i++) = ... a... (or)
8
by: Sing | last post by:
Dear C Gurus, I would like to optimise a max() algo that my program uses many times. Which one is faster or are they the same? 1. #define max(a,b) (((a) (b)) ? (a) : (b)) 2. if (a>b) return a...
36
by: lak | last post by:
Which is faster? Post Increment or assignment? Why? I was not able to get any things.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.