Which is faster?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aggelos I. Orfanakos

    #1

    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.

  • Aldo Cortesi

    #2
    Re: Which is faster?

    Thus spake Aggelos I. Orfanakos (aorfanakos@gma il.com):
    [color=blue]
    > 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.[/color]

    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:

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

    [color=blue]
    > python ./timeit.py "'a/b/c/'.rstrip('/')"[/color]
    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
    aldo@nullcube.c om

    Off: (02) 9283 1131
    Mob: 0419 492 863

    Comment

    • Aggelos I. Orfanakos

      #3
      Re: Which is faster?

      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.

      Comment

      • Terry Reedy

        #4
        Re: Which is faster?


        "Aggelos I. Orfanakos" <aorfanakos@gma il.com> wrote in message
        news:1106793602 .782397.61260@z 14g2000cwz.goog legroups.com...[color=blue]
        > Any idea which of the following is faster?
        >
        > 'a/b/c/'[:-1]
        > 'a/b/c/'.rstrip('/')[/color]

        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 ;-)
        [color=blue]
        > 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.[/color]

        For more on the CPython (2.2) reason, consider
        [color=blue][color=green][color=darkred]
        >>> def f1(s): return s[:-1][/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> def f2(s): return s.rstrip('/')[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> import dis
        >>> dis.dis(f1)[/color][/color][/color]
        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[color=blue][color=green][color=darkred]
        >>> dis.dis(f2)[/color][/color][/color]
        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




        Comment

        • Peter Otten

          #5
          Re: Which is faster?

          Aggelos I. Orfanakos wrote:
          [color=blue]
          > Any idea which of the following is faster?
          >
          > 'a/b/c/'[:-1]
          >
          > or
          >
          > 'a/b/c/'.rstrip('/')[/color]

          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

          Comment

          Working...