473,503 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1585
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
6102
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
5834
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
1642
by: junky_fellow | last post by:
which of the following is faster and why ? if ( x == 1 ) { } or if ( x != 0 ) {
65
12507
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
15001
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
1516
by: Sunil Varma | last post by:
Is accessing function by it's name faster or accessing it by its address faster?
4
2638
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
1694
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
3369
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
336
by: lak | last post by:
Which is faster? Post Increment or assignment? Why? I was not able to get any things.
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.