iteration without storing a variable

Discussion in 'Python' started by Josh Dukes, Mar 25, 2009.

  1. Josh Dukes

    Josh Dukes Guest

    So The metasploit framework was suffering from some performance issues
    which they fixed. http://www.metasploit.com/blog/

    I was interested in comparing this to python. Language comparisons are
    not generally very useful for a number of reasons, but some might find
    this interesting.

    Clearly the python operator += is way faster than ruby's:

    $ time python -c 'a = "A";
    for r in xrange(100000): a += "A" '

    real 0m0.109s
    user 0m0.100s
    sys 0m0.010s

    $ time ruby -e 'a = "A"; 100000.times { a += "A" }'

    real 0m3.113s
    user 0m3.110s
    sys 0m0.010s


    one more zero and the difference is magnified even more:

    $ time python -c 'a = "A";
    for r in xrange(1000000): a += "A" '

    real 0m1.208s
    user 0m0.940s
    sys 0m0.270s

    $ time ruby -e 'a = "A"; 1000000.times { a += "A" }'

    ^C

    I wasn't patient enough to wait more than 30 seconds for ruby
    to finish.....
    If you use a python list instead of a string (and join at the end) you
    get even better performance:

    $ time python -c 'a = ["A"];
    for r in xrange(1000000): a.append("A")
    "".join(a)'

    real 0m0.889s
    user 0m0.870s
    sys 0m0.020s

    This seems to compare closely to the improved ruby code:

    $ time ruby -e 'a = "A"; 1000000.times { a << "A" }'

    real 0m0.920s
    user 0m0.920s
    sys 0m0.000s

    Interestingly enough this is even with the slight performance hit for initializing python vs. initializing ruby:

    $ time ruby -e ''

    real 0m0.008s
    user 0m0.010s
    sys 0m0.000s

    $ time python -c ''

    real 0m0.023s
    user 0m0.010s
    sys 0m0.020s

    Obviously speed isn't everything and I choose python because it's more
    clean and readable, but it is interesting. The main question this raises
    for me is, "is it possible to more closely replicate the ruby code and
    do a loop without variable assignment?"

    Of course that was quickly followed by, "does the variable assignment
    on each iteration really cause any kind of performance hit?" In
    reference to the difference between ruby and python, it seems the
    answer is no. I'm guessing that this is because the iteration number
    needs to be stored anyway during a loop, so making it availble has
    essentially zero cost (sound right?).

    $ time python -c 'for r in xrange(1000000): pass'

    real 0m0.210s
    user 0m0.210s
    sys 0m0.000s

    $ time ruby -e '1000000.times { }'

    real 0m0.259s
    user 0m0.250s
    sys 0m0.000s


    Anyone see anything I missed? Any additional info? Anyone get different
    results?

    --

    Josh Dukes
    MicroVu IT Department
    Josh Dukes, Mar 25, 2009
    #1
    1. Advertising

  2. Josh Dukes wrote:
    > $ time python -c 'a = "A";
    > for r in xrange(100000): a += "A" '
    >
    > real 0m0.109s
    > user 0m0.100s
    > sys 0m0.010s
    >
    > Anyone get different results?


    Sure:

    $ time python -c 'a = "A";
    for r in xrange(100000): a += "A" '

    real 0m0.140s
    user 0m0.132s
    sys 0m0.008s


    Stefan


    (BTW, this is using Python 2.5.2 on Ubuntu Linux etc.pp., but no-one's
    interested in these details anyway, right?)
    Stefan Behnel, Mar 25, 2009
    #2
    1. Advertising

  3. Josh Dukes

    Josh Dukes Guest

    well if we're interested in that...

    $ uname -a
    Linux IT2-JD 2.6.27-gentoo-r8 #1 SMP Tue Mar 17 14:28:19 PDT 2009 x86_64 Intel(R) Pentium(R) D CPU 2.80GHz GenuineIntel GNU/Linux

    $ python --version
    Python 2.5.2

    $ ruby --version
    ruby 1.8.6 (2008-08-11 patchlevel 287) [x86_64-linux]

    but I was more talking about the speed differences between ruby and python.

    On Wed, 25 Mar 2009 20:13:13 +0100
    Stefan Behnel <> wrote:

    > Josh Dukes wrote:
    > > $ time python -c 'a = "A";
    > > for r in xrange(100000): a += "A" '
    > >
    > > real 0m0.109s
    > > user 0m0.100s
    > > sys 0m0.010s
    > >
    > > Anyone get different results?

    >
    > Sure:
    >
    > $ time python -c 'a = "A";
    > for r in xrange(100000): a += "A" '
    >
    > real 0m0.140s
    > user 0m0.132s
    > sys 0m0.008s
    >
    >
    > Stefan
    >
    >
    > (BTW, this is using Python 2.5.2 on Ubuntu Linux etc.pp., but no-one's
    > interested in these details anyway, right?)
    > --
    > http://mail.python.org/mailman/listinfo/python-list



    --

    Josh Dukes
    MicroVu IT Department
    Josh Dukes, Mar 25, 2009
    #3
  4. Josh Dukes wrote:
    > $ python --version
    > Python 2.5.2
    >
    > $ ruby --version
    > ruby 1.8.6 (2008-08-11 patchlevel 287) [x86_64-linux]
    >
    > but I was more talking about the speed differences between ruby and python.


    I heard that Ruby 1.9 is supposed to be a lot faster than 1.8 in many
    aspects (as is Py2.6 compared to 2.5 or 2.4, BTW), so I think version
    numbers matter a lot in this case.

    Stefan
    Stefan Behnel, Mar 25, 2009
    #4
    1. Advertising

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. Hal Vaughan
    Replies:
    8
    Views:
    436
    M.J. Dance
    Sep 11, 2006
  2. toton
    Replies:
    11
    Views:
    704
    toton
    Oct 13, 2006
  3. Replies:
    9
    Views:
    617
    Barry
    May 30, 2008
  4. Jonathan Wood
    Replies:
    1
    Views:
    505
    Jonathan Wood
    Jun 2, 2008
  5. Rudi
    Replies:
    5
    Views:
    4,996
Loading...

Share This Page