Re: Bitwise OR?

Discussion in 'Python' started by Jean-Paul Calderone, Mar 24, 2006.

  1. On Fri, 24 Mar 2006 15:40:17 +0100, "Diez B. Roggisch" <> wrote:
    >Tim N. van der Leeuw wrote:
    >> I also wonder if it wouldn't be faster to put the numbers into a list
    >> and join the list into a string -- did you test with that?

    >
    >It will be faster - the naive string concatenation is quadratic, whereas the
    >list-based approach is linear.


    It won't be any faster in CPython 2.4 or newer. This kind of string concatenation is optimized so as to be linear:

    $ python -m timeit -s "x = ''" "for i in xrange(1000): x += 'x'"
    1000 loops, best of 3: 335 usec per loop
    $ python -m timeit -s "x = ''" "for i in xrange(10000): x += 'x'"
    100 loops, best of 3: 3.35 msec per loop
    $ python -m timeit -s "x = ''" "for i in xrange(100000): x += 'x'"
    10 loops, best of 3: 33.2 msec per loop
    $ python -m timeit -s "x = ''" "for i in xrange(1000000): x += 'x'"
    10 loops, best of 3: 347 msec per loop
    $ python -m timeit -s "x = ''" "for i in xrange(10000000): x += 'x'"
    10 loops, best of 3: 3.48 sec per loop
    $

    Jean-Paul
     
    Jean-Paul Calderone, Mar 24, 2006
    #1
    1. Advertisements

  2. > It won't be any faster in CPython 2.4 or newer. This kind of string
    > concatenation is optimized so as to be linear:
    >
    > $ python -m timeit -s "x = ''" "for i in xrange(1000): x += 'x'"
    > 1000 loops, best of 3: 335 usec per loop
    > $ python -m timeit -s "x = ''" "for i in xrange(10000): x += 'x'"
    > 100 loops, best of 3: 3.35 msec per loop
    > $ python -m timeit -s "x = ''" "for i in xrange(100000): x += 'x'"
    > 10 loops, best of 3: 33.2 msec per loop
    > $ python -m timeit -s "x = ''" "for i in xrange(1000000): x += 'x'"
    > 10 loops, best of 3: 347 msec per loop
    > $ python -m timeit -s "x = ''" "for i in xrange(10000000): x += 'x'"
    > 10 loops, best of 3: 3.48 sec per loop



    Interesting. I wasn't aware of that optimization.

    diez
     
    Diez B. Roggisch, Mar 24, 2006
    #2
    1. Advertisements

  3. Jean-Paul Calderone

    Jorge Godoy Guest

    "Diez B. Roggisch" <> writes:

    > Interesting. I wasn't aware of that optimization.


    It's better not count on it. It's not there for Jython, IronPython, previous
    versions of Python, etc. It is just there for the new cPython. And the fix
    is so simple that it isn't worth disregarding all those other versions.

    --
    Jorge Godoy <>

    "Quidquid latine dictum sit, altum sonatur."
    - Qualquer coisa dita em latim soa profundo.
    - Anything said in Latin sounds smart.
     
    Jorge Godoy, Mar 24, 2006
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dohnut
    Replies:
    0
    Views:
    658
    dohnut
    Oct 20, 2003
  2. dohnut
    Replies:
    1
    Views:
    717
    Sam Holden
    Oct 21, 2003
  3. Random

    bitwise comparator

    Random, Jun 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,350
    Random
    Jun 8, 2005
  4. =?Utf-8?B?Sm9u?=

    BitWise Operations

    =?Utf-8?B?Sm9u?=, Jan 23, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,572
    =?Utf-8?B?Sm9u?=
    Jan 24, 2006
  5. Pasquale Imbemba

    Bitwise Operation

    Pasquale Imbemba, May 6, 2004, in forum: Java
    Replies:
    2
    Views:
    710
    Roedy Green
    May 7, 2004
  6. theo

    bitwise problems&dpcm

    theo, May 16, 2005, in forum: Java
    Replies:
    2
    Views:
    459
  7. -
    Replies:
    11
    Views:
    892
    Dale King
    Jul 16, 2005
  8. -

    Help with bitwise

    -, Jul 6, 2005, in forum: Java
    Replies:
    4
    Views:
    1,587
    Yamin
    Jul 7, 2005
Loading...