Ruby Performance Revisited 6
For this post I used a code snippet I found from a fellow programmer, Antonio Cangiano, that ran the tests again myself because I couldnt believe my eyes of the benchmarks run by that user.
Fibonacci Language Shootout:
Languages
- Ruby 1.8.6
- Ruby 1.9.0 (Development Release)
- Python 2.5.1
- Perl 5.8.8
- Java 1.5.0
- C++
Below are the languages and the times that each took to run the code. The code for each languages is below near the end of the post.
Ruby 1.8.6
real 0m44.965sPython 2.5.1
real 0m28.283sRuby 1.9.0
real 0m11.352sC++
real 0m0.765sJava
real 0m0.638sPerl
real 0m70.383sI will be comparing this performance of Ruby and Python to a program written in C. Below is the code used in these examples. Feel free to comment. I am running further tests using statistical analysis to make the output exhibit less of a standard deviation.
Python
def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)
for i in range(36):
fib(i)Ruby
def fib(n)
if n == 0 || n == 1
n
else
fib(n-1) + fib(n-2)
end
end
36.times do |i|
fib(i)
endBelow is the source code for the fib sequence written in C++. Paul Solt recommended that the tests be performed without and writing to STDOUT due to the variability and slow down caused by writing to STDOUT. Since this was done for the C++ code it was done for the rest of the examples above. For the record there was no increase in speed when the output was removed.
C++
#include <stdio.h>
#include <iostream>
int fib( int n ) {
if( n== 0 || n == 1 ) {
return n;
} else {
return fib( n -1) + fib( n-2);
}
}
int main() {
for( int i = 0; i < 36; i++ ) {
fib(i);
}
return 0;
}Java
public class fibtest {
public static void main(String[] args) {
for(int i=0; i<36; i++) {
fib(i);
}
}
public static int fib(int n) {
if( n == 0 || n == 1 )
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
}Perl
sub fib { return $[0] if $[0] == 0 || $_[0] == 1; fib($[0]-1) + fib($[0]-2); }
for($i=0;$i<36;$i++) { fib($i); }I would like to extend my thanks to the developers that submitted, or contributed to this post in anyway.
Java & Perl Code - Jason Koppe
Ruby Broken on Leopard [Not Really] 1
Introduction
I have a reason to believe that Ruby is broken yet again on Leopard. That being said there are a few options ruby developers have to fix said issue.
I encourage reader comments on this issue...
irb
require 'rubygems'
=> false
As you can see rubygems fails to load.
"Solved"
As stated below by Dan's comment, ruby is not shipped broken but it is setup in such a way that would obfuscate itself to the traditional ruby developer. That is because it is setup by Apple in a framework. The traditional locations for irb, ruby, etc are just symlinks. Reinstalling the snmp gem fixed the issue. I am looking into the issue I had further to figure out why was it throwing the exception in the first place when I was trying to load the snmp gem.
Terminal Emulation on Mac OS X Leopard 2
The best and fastest solution to have a solid terminal emulation product that is free on Mac OS X Leopard is to just use minicom.
sudo port install minicom
sudo minicom -s
Configure minicom to your liking. If you need to find you device to configure minicom with just run
ls /dev | grep tty.*
tty.usbserial
Add /dev/tty.usbserial to the modem line in the configuration. Save the config as default and select exit
TextMate & Logitech Control Center
Just another reason to never use crappy drivers, TextMate throws and error when run from the command line after Logitech Control Center 2.4 is installed. Remove the drive via the "LCC Uninstaller" and reboot. After that everything will be hunky dory again.
Note: There are three other good alternatives to that mouse driver including SteerMouse, USBOverdrive, and ControllerMate.
Ruby on Rails with ImageMagick and rmagick on Leopard
Introduction
In the past it has been somewhat interesting to install rmagick and imagemagick on leopard. This is because of the tiff port in MacPorts failing to compile. I prefer MacPorts for jobs like this because if there are any security vulnerabilities in ImageMagick I would easily be able to update it and any of its dependencies with MacPorts and it would not be as easy when compiling from source. That being said lets move on.
Requirements
- Fully Patch Mac OS X Leopard 10.5.1
- Apple Developer Tools
- Updated Ruby on Rails
- MacPorts
Procedure
Once you have met all the requirements above you will need to open a Terminal and run the following commands.
# Make sure that MacPorts is fully updated
sudo port -d selfupdate
sudo port sync# Install ImageMagick
sudo port install ImageMagick# Install rmagick
sudo gem install rmagick