loop in python

Discussion in 'Python' started by Rick Wotnaz, Aug 22, 2005.

  1. Rick Wotnaz

    Rick Wotnaz Guest

    km <> wrote in
    news::

    > Hi all,
    >
    > Why is it that the implementation of empty loop so slow in
    > python when compared to perl ?
    >
    > #i did this in python (v 1.5)
    > for x in xrange(1000):
    > print x
    > # this took 0.017 seconds
    > --------------------------
    > #similar code in perl (v 5.6):
    > for $x (0..1000)
    > {
    > print $x;
    > }
    > # this took 0.005 seconds only !!!
    >
    > Is python runtime slow at all aspects when compared to perl ?
    > I really wonder what makes python slower than perl ?
    > Is there any proposal to make python faster in future versions ?
    >
    > curious to know all these ...
    >


    It appears that Python is not optimized for empty loops.

    --
    rzed
    Rick Wotnaz, Aug 22, 2005
    #1
    1. Advertising

  2. km wrote:
    > Hi all,
    >
    > Why is it that the implementation of empty loop so slow in python when compared to perl ?
    >
    > #i did this in python (v 1.5)


    Python 1.5.2 was released in april 1999. Current Python version is 2.4.1.

    Please consider upgrading - unless of course you just want to troll...

    > for x in xrange(1000):
    > print x


    This is not an empty loop. An empty loop is
    for x in xrange(1000):
    pass

    > Is python runtime slow at all aspects when compared to perl ?


    Is your question serious or are you just trolling ?

    > I really wonder what makes python slower than perl ?


    I really wonder what makes my old P133/32mo running w98 slower that my
    more recent XP1200/256mo running gentoo/linux.

    > Is there any proposal to make python faster in future versions ?


    The future is already here....


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno modulix, Aug 22, 2005
    #2
    1. Advertising

  3. km wrote:
    > Why is it that the implementation of empty loop so slow in python when compared to perl ?

    [...]
    > Is python runtime slow at all aspects when compared to perl ?


    No

    > I really wonder what makes python slower than perl ?


    It could be that the Perl compiler recognizes such a for loop and emits
    special code (e.g. an integer loop variable that gets incremented).
    Python creates an xrange object and then has the overhead of 1000 next()
    calls.

    You should benchmark other features before jumping to conclusions: calls
    to functions, calls to methods, calls to inherited methods.

    Others will probably chime in with arguments whether such micro
    benchmarks are a useful indication of the speed a complete program at all.

    > Is there any proposal to make python faster in future versions ?


    Yes in the general case, probably no in this specific case.


    Daniel
    Daniel Dittmar, Aug 22, 2005
    #3
  4. Rick Wotnaz

    D H Guest

    km wrote:
    > Hi all,
    >
    > Why is it that the implementation of empty loop so slow in python when compared to perl ?
    >
    > #i did this in python (v 1.5)
    > for x in xrange(1000):
    > print x
    > # this took 0.017 seconds
    > --------------------------
    > #similar code in perl (v 5.6):
    > for $x (0..1000)
    > {
    > print $x;
    > }
    > # this took 0.005 seconds only !!!
    >
    > Is python runtime slow at all aspects when compared to perl ?
    > I really wonder what makes python slower than perl ?


    Yeah but the python version took 2.1 less seconds to type.
    Python runs as fast as Perl.
    D H, Aug 22, 2005
    #4
  5. Rick Wotnaz

    Randy Bush Guest

    computers are cheap. i am expensive. give me clear and maintainable
    code every time.

    randy
    Randy Bush, Aug 23, 2005
    #5
  6. Rick Wotnaz

    Gregory Bond Guest

    km wrote:

    > Is python runtime slow at all aspects when compared to perl ?


    And in addition to all that everyone else has said.... most of this is
    startup time. The python interpreter is a fair bit slower to start up
    (i.e. does much more at startup time) than the perl one:

    > lenford$ time python -c "pass"
    >
    > real 0m0.298s
    > user 0m0.030s
    > sys 0m0.030s
    > lenford$ time perl -e "0"
    >
    > real 0m0.037s
    > user 0m0.000s
    > sys 0m0.000s


    [on a fairly fast Solaris box]. This is mainly because much more of
    python is written in python and needs to be loaded / dynamically linked
    when the process starts up. On my system, python loads 14 .so files and
    13 .pyc files. Perl loads 7 .so files.
    Gregory Bond, Aug 23, 2005
    #6
  7. Rick Wotnaz

    km Guest

    Hi all,

    Why is it that the implementation of empty loop so slow in python when compared to perl ?

    #i did this in python (v 1.5)
    for x in xrange(1000):
    print x
    # this took 0.017 seconds
    --------------------------
    #similar code in perl (v 5.6):
    for $x (0..1000)
    {
    print $x;
    }
    # this took 0.005 seconds only !!!

    Is python runtime slow at all aspects when compared to perl ?
    I really wonder what makes python slower than perl ?
    Is there any proposal to make python faster in future versions ?

    curious to know all these ...

    regards,
    KM
    km, Aug 23, 2005
    #7
    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. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,381
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,230
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    691
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    415
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page