5 ms·
Introducing Surinx (Ruby syntax w/ Java typing)
- apgwoz 17y ago> ... fib(40) in only 7 seconds on current MLVM (OpenJDK7 + invokedynamic and other tidbits), a five-fold improvement over JRuby's fastest mode (jruby --fast). 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.
- lliiffee 17y agoDoes fib.py use psyco? If not, that does still seem slow. Standard numerical benchmarks indicate that c or fortran or whatever should have a much larger speed advantage relative to regular python than 40/163. E.g. on the n-body benchmark here: http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&lang=all http://shootout.alioth.debian.org/u32/benchmark.php?test=nbo... Python is about 65 times slower than c/c++/java/fortran/scala.
- apgwoz 17y agoNo, it doesn't use psyco. If it did, I'd expect it to be faster of course. 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.
- ginkgo 17y agoOr you could simply make your python code tail-recursive: $ time python fib.py 40 102334155 real 0m0.013s user 0m0.000s sys 0m0.010s
- carbon8 17y agoThen you are testing two different things, and if you use tail recursion even Ruby 1.9 still runs it faster than Python. # 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.011s
- apgwoz 17y agoOf course you can, but that's not going to work when you run out of stack space. The point of my stupid benchmark was to write the equivalent ruby code, and then the equivalent C code, forgetting the fact that there's a much more optimal way to do it (e.g. write it in scheme with tail calls).
- apgwoz 17y ago> was to write the equivalent ruby code, that should have stated "equivalent python code"
- steveklabnik 17y agoPython does not do tail call optimization.
- gloob 17y agoWhether it does tail call optimization is a different question than whether you can write the algorithm in a tail-recursive manner, though.
- steveklabnik 17y agoYes, but without the optimization, how would it help? Am I just being a n00b?
- apgwoz 17y agoNo, you're not being a n00b. It'll help because the stack usage will grow linearly as opposed to quadratically, as in the case of the naive solution. You'll still overflow the stack without the optimization.
- lliiffee 17y agoI just tried it with psyco. I get about 103 seconds with normal python and about 4 seconds with psyco. (Including the imports.)
- mey 17y agoOr you could just use Groovy.
- raganwald 17y agoThe Fibonacci implementation here: http://github.com/raganwald/homoiconic/blob/master/2008-12-12/fibonacci.md#readme 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!"
- lliiffee 17y agoThat basically uses dynamic programming, which is "cheating". (It is a linear time algorithm instead of exponential!) Different compilers will almost always just give us constant factor improvements. Smarter algorithms will always beat that, but sometimes a smart algorithm with a slow compiler still isn't fast enough.
- raganwald 17y agoI don't think the matrix algorithm is linear, in other words I don't think fib(40) takes exactly twice as long to compute as fib(20). I think it's faster than linear, because it converts the multiplications to exponentiation and then takes advantage of composeability such that fib(40) should require on the order of one more operation than fib(20). I think that means it's order log 2 n, but perhaps you HN folks can correct my poor math skills.
- lliiffee 17y agoThe site is down now, but from looking at wikipedia, you are probably right. (Sorry, I just glanced at the way they were using the matrix before, and assumed they did n matrix multiplies to calculate fib(n)) I think you are also right about the complexity, though people would usually just write log(n) since log(a*b)=log(a)+log(b).
- lucifer 17y agoThere are so many (new) languages for the JVM; its becoming a curse of plenty! (Not complaining.) Its a great platform, and invokedynamic will usher in a whole new generation of languages. 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 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.)
- donw 17y agoI never stop being impressed by Charles (the JRuby lead dev)... remind me to ship a case of the Sierra Nevada Hefeweitzen to Engine Yard. 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...)
- rogerthat 17y agoYou are reminded to ship a case of the Sierra Nevada Hefeweitzen to Engine Yard.
- jrockway 17y agoJava has a type system?
- megaduck 17y agoOne of the beautiful things about JRuby is that for performance-intensive code, you can drop to Java instead of dropping to C. Java's almost as fast, and a whole heck of a lot nicer to program in. 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.
- megaduck 17y agoOne thing to remember about a lot of JVM language benchmarks is that they include the startup time for the JVM. Java code running on a pre-warmed JVM is significantly faster, and for short programs can be faster by an order of magnitude (or two). For a good example, check out Nailgun (http://martiansoftware.com/nailgun/background.html http://martiansoftware.com/nailgun/background.html).
- snprbob86 17y agoBoo is a similar project with Python and .NET: http://boo.codehaus.org/ http://boo.codehaus.org/
- deleted 17y ago[deleted]