Ruby Performance Revisited 6

Posted by Ron Valente Thu, 21 Feb 2008 04:28:00 GMT

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.965s

Python 2.5.1

real 0m28.283s

Ruby 1.9.0

real 0m11.352s

C++

real 0m0.765s

Java

real 0m0.638s

Perl

real 0m70.383s

I 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)
end

Below 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.

Ruby & Python Code

Paul Solt - C Developer

Java & Perl Code - Jason Koppe

Terminal Emulation on Mac OS X Leopard 2

Posted by Ron Valente Tue, 19 Feb 2008 01:27:00 GMT

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

Creating a System Account in Mac OS X 10.5 Leopard

Posted by Ron Valente Fri, 19 Oct 2007 20:11:00 GMT

Introduction

With the upcoming release of leopard there are many changes under the hood. One of the small but quite annoying changes if you are used to the old ways is the removal of the NetInfo manager application. This is a blessing in disguise because the new implementation of user creation and account management.

Create the users home directory

sudo mkdir -p /Users/USER

Creating the users primary group

sudo dscl . -create /Groups/USER
sudo dscl . -create /Groups/USER PrimaryGroupID UID

Creating the user and setting options

sudo dscl . -create /Users/USER
sudo dscl . -create /Users/USER RealName "USER FULL NAME"
sudo dscl . -create /Users/USER NFSHomeDirectory /Users/USER
sudo dscl . -create /Users/USER UserShell /bin/SHELL
sudo dscl . -create /Users/USER UniqueID UID
sudo dscl . -create /Users/USER PrimaryGroupID UID

Setting the users passwd

passwd USER

Adding users to a secondary group

sudo dscl . -append /Groups/OTHERGROUP GroupMembership USER

Set the ownership of the users home directory to the users home dir

sudo chown -R USER:GROUP /Users/USER