iteration without storing a variable

J

Josh Dukes

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?
 
S

Stefan Behnel

Josh said:
$ 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?)
 
J

Josh Dukes

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.
 
S

Stefan Behnel

Josh said:
$ 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top