|
|
Ruby 1.9 doesn't smoke Python away!In his blog entry "Holy Shmoly, Ruby 1.9 smokes Python away!" Antonio proposed following Python benchmarks for calculating first 36 Fibonacci numbers.
Antonio noticed that on his workstation above code was executed for 31.507s and analog in Ruby 1.9 – 11.934s. On my 4-years old workstation same code took 60.656s, but what time we will see if same 36 Fibonacci numbers with we will find with Python generators?
Well, my old workstation shown me for above code 0.078s, so almost in 1000 times faster (and please note 0.078s include time for reading and parsing python source by python interpreter). As Antonio wrote: And it's hard to disagree with Antonio, it's really interesting to see where Ruby manages to knock Python out of the water. P.S. In the memory of Java, which was my favorite language before Python, I made same Fibonacci test on same workstation and it was executed for 0.808s. Not so bad, by the way, but it's so hard code in Java after Python.
|
Web log, research lab and soft parade of Dima Dogadaylo.
Email: entropyhacker at gmail dot com |
| [about | blog | projects]
© Dima Dogadaylo |
|
def fib (n, &block)
a, b = 0, 1
i = 0
while i #{f}" }
time that one to make the comparison fair.
def fib (n, &block)
a, b = 0, 1
i = 0
while i < n
yield [i, a]
a, b = b, a + b
i += 1
end
end
fib(36) { |i, f| puts "n=#{i} => #{f}" }
fib = lambda {|n,b| i,k=0,1; n.times {|p| b[p, i]; i,k=k,i+k } }
fib[36, proc {|i, f| puts "n=%d => %d" % [i, f] }]
...which runs in about 1/2 the time of the Python generator-based solution above on my workstation.
To be fair, the difference is much smaller when you increase the number of iterations to 1000 or so, which suggests to me that the overhead of launching the interpreter is probably the biggest use of time in these trivial examples.
For the real performance much more important developers skills than programming language benchmarks.
@cache = []
def fib(n)
return @cache[n] if @cache[n]
return n if n < 2
return @cache[n] = fib(n-1) + fib(n-2)
end
36.times {|n| puts "fib(#{n}) = #{fib(n)}" }
Or, how about this one-liner that runs is 0.008 seconds:
@c=[]; def f(n) if !@c[n]; return n if n<2; return @c[n]=f(n-1)+f(n-2) end; @c[n] end; 1.upto(36) {|n| puts f(n)}
IMHO this language war lead to great language improve to beat the other therefore is great!!! but you can choose whether python or ruby and solve most of your projects using them. I'd much rather python than ruby but this is my personal view. There is no objective reason.
So quit dick measurement contest and get laid or something.
ROTFL! so true
someones suggested that my favorite language {ruby || python || java || c# } isn't the {best || fastest || coolest } in some arbitrary code examplez
I mustz run to itz defenze
sigh....
on a related note - oh look I just deleted another site from my RSS reader for being lame.
http://pastie.caboo.se/123814
In my Macbook time goes down to 0.004
At just 36 times Ruby blows Java away, of course. The JVM startup alone kills the test, but for 10.000 times speed catches up and Java and Ruby ends up having roughly the same performance.
And it bears mentioning that to me the most important note in that post was that Ruby 1.9 is doing so much better than Ruby 1.8. Python's performance is less important.
I guess what I'm trying to say is, we should continue to compare and contrast the two languages so that they can both get better and stop bickering when one happens to do better at "X" than the other one.
BTW, code speed / stability is 90% developer skill, not language features. They are all too close to compare based on that any longer.
+1
Esli uzh na to poshlo, davaj zapolnim massiv vychislennymi rezul'tatami fib[0..100] i proga budet voobwe superbystroj. CHto jeto dokazyvaet? CHto Dima ne ponimaet smysla.
Then he tried the same deliberately-lame algorithm in Python, to see how well it did checking the same things. The benchmark isn't for how fast he can generate Fibonacci numbers (he's not that stupid)--it's for how fast the new Ruby can call methods and handle its stack.