I'm also pleased to see how well 1.9 is advancing. But it is still
possible to be faster. Here's another silly micro-benchmark...
==== bench.rb ====
require 'benchmark'
def test(n)
n.times {
h = Hash.new
a = %w{a b c d e f g h i j k l m n
o p q r s t u v w x y z}
a.each { |l| h[l] = l.upcase }
p =
h.each { |k,v| p << [k, v] }
p.each { |p1,p2| h.delete(p1) }
}
end
Benchmark.bm { | x |
x.report { test(100000) }
}
···
On Nov 28, 4:24 am, Antonio Cangiano <acangi...@gmail.com> wrote:
I ran a micro-benchmark with Ruby 1.8.6, Ruby 1.9 and Python 2.5.1,
and was stunned by Ruby 1.9's improvements. Incidentally for my
particular test (which is a silly one...), Ruby was more than twice as
fast as Python!
http://antoniocangiano.com/2007/11/28/holy-shmoly-ruby-19-smokes-pyth\.\.\.
We can't say for sure yet, but chances are that Ruby 1.9 is going to
be faster than CPython in most situations, which would be a great
accomplishment and step forward for the Ruby community. It's not a
competition, but a speed boost of this caliber is super welcomed.
Cheers,
Antonio
==================
==== bench.py ====
from cProfile import run
def test(n):
for i in xrange(n):
h = dict()
a = 'a b c d e f g h i j k l m n ' \
'o p q r s t u v w x y z'
for l in a.split():
h[l] = l.upper()
p =
for k, v in h.items():
p.append([k, v])
for p1, p2 in p:
del h[p1]
run('test(100000)')
Results:
$ ruby -v && ruby bench.rb
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-linux]
user system total real
24.340000 1.920000 26.260000 ( 28.686532)
$ ruby19 -v && ruby19 bench.rb
ruby 1.9.0 (2007-10-15 patchlevel 0) [i686-linux]
user system total real
17.570000 0.050000 17.620000 ( 19.293883)
$ python -V && python bench.py
Python 2.5.1
5400003 function calls in 14.798 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall
filename:lineno(function)
1 0.000 0.000 14.798 14.798 <string>:1(<module>)
1 11.015 11.015 14.798 14.798 bench.py:3(test)
2600000 1.447 0.000 1.447 0.000 {method 'append' of
'list' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profiler' objects}
100000 0.279 0.000 0.279 0.000 {method 'items' of
'dict' objects}
100000 0.509 0.000 0.509 0.000 {method 'split' of 'str'
objects}
2600000 1.548 0.000 1.548 0.000 {method 'upper' of 'str'
objects}
So 1.8.6 comes it at 28.686532 seconds.
Version 1.9 shows a 33% improvement at 19.293883.
But python still takes the cake at 14.798 (49% improvement).
But like others have said, people don't use ruby because it's the
fastest language in the world. A beautiful person may be stupid, and a
smart person may be ugly; but no one ever asked to paint a portrait of
an ugly person, no matter how smart they were. Still, the fact that
current ruby 1.9 performed within 16% of python in this bechmark is
nice.
Regards,
Jordan