3 ms·
Ruby Fibonacci Shootout
- idlewords 17y agoBased on this article, I would totally consider moving to Ruby for my Fibonacci number needs.
- hazzen 17y agoI don't think this "CPU Intensive" task is the kind of CPU-intensive task you actually care about. I haven't done any formal experimentation, but I'm guessing this is a better test of how many instructions are generated for a call/ret sequence, and not how well normal CPU-intensive operations would work. I would think some code that did a fair amount of number crunching/lookups in a tight loop would be a much better test. But, then again, it doesn't really matter. Is Ruby fast enough to work for most uses? Yes. If you really, really, really care about execution speed, would you use Ruby? I don't know. If you care that much about execution speed, you will probably do more benchmarks than I care to think of.
- sailormoon 17y agoHuh? Where's the results? Or are readers supposed to run the code and send their results or something? But there's no timing check in the code he posted anyway.
- Freaky 17y agoThey don't render in IE (tested in 8); it cuts the article in half.
- sailormoon 17y agoOr firefox mac. OK, now I looked at it through safari and I can see it.
- warfangle 17y agoPorted this to scala (I know, I didn't do this functionally, this is a pretty straight port of the C example), ran in 4 seconds (of course, I've got about a bajillion other apps open right now, so take this with a grain of salt): object Main { def fib(n : int) : int = { if(n < 2) n else fib(n-1) + fib(n-2) } def main(args: Array[String]) = { for (i <- 0 until 37) { printf("n = " + i.toString + " => "+fib(i).toString) } } }
- sreque 17y agoOn my 1.4ghz core 2 duo laptop, which isn't a very powerful machine, the above scala code runs in about 1 second when compiled. If I let the code run once to to let the JIT warm up and only measure the execution of the main body, it runs in .60 seconds on average. By comparison, gcc with -O3 runs the program in about .57 seconds. I don't know how you got 4 seconds unless you ran the program as a script and had a lot of other stuff running simultaneously. A simple scala hello world program on my machine has a startup time of 1.5 seconds when run as a script.
- CaseOfEmergency 17y agoI kept staring at this page, and yet despite its brevity, I could not find the results. Apparently Firefox doesn't like the self-closing script tag?
- sailormoon 17y agoIs that valid? Also the script is missing the "type" attribute.
- xal 17y agoboth valid HTML5
- sailormoon 17y agoInteresting, thanks. Looks like FF has some catching up to do.
- InclinedPlane 17y agoRecursive/iterative Fibonacci number generation is an interesting programming exercise but it's a bad example because in practice you'd be best to use an explicit constant-time formula.
- deleted 17y ago[deleted]
- bensummers 17y agoIs it really a shootout if he misses most of the Ruby implementations?