473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

loop in python

km
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

Aug 22 '05 #1
6 1858
km <km@mrna.tn.nic.in> wrote in
news:ma***************************************@pyt hon.org:
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
Aug 22 '05 #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 'o****@xiludom.gro'.split('@')])"
Aug 22 '05 #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
Aug 22 '05 #4
D H
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.
Aug 22 '05 #5
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.

Aug 23 '05 #6
computers are cheap. i am expensive. give me clear and maintainable
code every time.

randy

Aug 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: beliavsky | last post by:
In a Python 'for' loop, one can change the value of looping variable, so that for i in range(3): i = i*5 print i is legal code In Fortran 90 and 95, the analogous code
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
7
by: houbahop | last post by:
Hello, I have seen a lot of way to use the for statement, but I still don't know how to do: for i=morethanzero to n or for i= 5 to len(var) of course I kno wthat I can use a while loop to...
4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
6
by: Gary Wessle | last post by:
Hi using the debugger, I happen to be on a line inside a loop, after looping few times with "n" and wanting to get out of the loop to the next line, I set a break point on a line after the loop...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
8
by: malkarouri | last post by:
Hi everyone, I have an algorithm in which I need to use a loop over a queue on which I push values within the loop, sort of: while not(q.empty()): x = q.get() #process x to get zero or more...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.