Introducing Surinx (Ruby syntax w/ Java typing)
blog.headius.com
blog.headius.com
http://github.com/raganwald/homoiconic/blob/master/2008-12-1...
Does a pretty quick job using plain-old-old-ruby (POOR Implementation):
time ruby /Users/raganwald/Desktop/matrix_fib.rb 102334155
real 0m0.012s
user 0m0.007s
sys 0m0.004s
That doesn't mean we shouldn't welcome implementation improvements, but in my lifetime the quest for a "sufficiently smart compiler" that will do away with thinking programmers has enjoyed only modest success.Append: This is not a criticism of Charles' amazing work dragging Ruby into the 1980s and designing Ruby-like languages for the JVM. If asked to choose between algorithm improvements and implementation improvements, I answer "Yes!"
I think that means it's order log 2 n, but perhaps you HN folks can correct my poor math skills.
The algorithm is remarkably simple. Given that fib(40) can be extracted from:
40
[ 1 1 ]
[ 1 0 ]
The simplest implementation, as you surmise, is to do 40 matrix multiplications. I don't think that part is dynamic. the dynamic part comes in observing that n^40 = (n^20)^2. This can be followed downward recursively: n^20 = (n^10)^2, n^10 = (n^5)^2, n^5 = (n^2)^2 * n and n^2 = n * n.So... The actual matrix multiplication is much slower than a simple bignum multiplication, but there are only six such operations to perform.
See Performance and Evaluation of Lisp Systems, http://www.dreamsongs.com/Files/Timrep.pdf, for more discussions of the nature of benchmarks.
You may find it irrelevant to the author's point, others may find it an interesting byway to follow in a larger conversation. Or not.
I know, because I hack on Guile ;-) Fortunately in this case we come in at about 20 seconds. But there's a long way to go. I'm tired of the "slow language" question, and of having to drop down to C to do even modest number crunching.
And by "drop down to C", I really mean anything that's not written in the HLL (Scheme in my case) -- I'm less and less interested even by C libraries providing Scheme functions.
The point of Duby/Juby is to give us _more_ control over these algorithms by dragging them out of the base implementation language and into Ruby. Why should inject be written in C or Java?
I agree with your point and bless you with a mighty upmod. One of my peeves about inject being written in C is that there is very little pressure to make Ruby fast enough that inject could be written in Ruby. So who cares? Well, if you want to make your own language feature that is like inject but not inject, your feature will be 100x slower.
So making Ruby fast enough that Ruby can be implemented in Ruby is a huge win for metaprogrammers :-)
> Now it is not correct to say that Matz does not use Ruby. He does, and so he does eat his own dog food. And for the domains where he uses Ruby, he has optimized Ruby to be a useful tool. But since he doesn’t use Ruby to build Ruby, he does not have the same incentive to tune Ruby for the purpose of building languages.
http://weblog.raganwald.com/2006/11/significance-of-meta-cir...
If Surinx is able to produce Java-like speeds, that means that you never have to drop out to a different language at all. That's pretty awesome, and it would open up a whole new world to Ruby programmers.
I hope some of this work gets pushed into JRuby. I'll take 5x speed boosts any day, constant factor or not.
I was going to say, "damn, that's it? that's pretty slow" until I realized:
$ time python fib.py 40
102334155
real 1m43.363s
user 1m36.824s
sys 0m0.577s
It may be the fact that I have a ton of stuff running on my machine right now, but still.http://shootout.alioth.debian.org/u32/benchmark.php?test=nbo...
Python is about 65 times slower than c/c++/java/fortran/scala.
I just rewrote it in C though:
$ time ./fib 40
102334155
real 0m3.063s
user 0m2.912s
sys 0m0.014s
Which makes the python version something like 35x slower in wall clock time. $ time python fib.py 40
102334155
real 0m0.013s
user 0m0.000s
sys 0m0.010s # 400
$ time ruby fib.rb
real 0m0.017s
user 0m0.010s
sys 0m0.006s
# 400
$ time python fib.py
real 0m0.023s
user 0m0.011s
sys 0m0.011sthat should have stated "equivalent python code"
For a good example, check out Nailgun (http://martiansoftware.com/nailgun/background.html).
The best thing is that this will likely percolate into JRuby, and it bodes well for JRuby performance on JVMs that support invokeDynamic (not sure if OpenJDK does yet...)
Lets just hope the Java 7 actually happens given the change of ownership, and I would love to see John Rose's call for programmatic access from Java (http://blogs.sun.com/jrose/entry/jsr_292_support_in_javac) realized. (ASM is great stuff, but I'll admit that its not a great joy to use for me.)