blog qmail quick facts connection problems free dns short urls open dns http proxy linux news 
  2005-05 
  2005-06 
  2005-07 
  2005-08 
  2005-09 
  2005-10 
  2005-11 
  2005-12 
  2006-01 
  2006-02 
  2006-03 
  2006-04 
  2006-05 
  2006-06 
  2006-07 
  2006-08 
  2006-09 
  2006-10 
  2006-11 
  2006-12 
  2007-01 
  2007-02 
  2007-03 
  2007-04 
  2007-05 
  2007-06 
  2007-07 
  2007-08 
  2007-09 
  2007-10 
  2007-11 
  2007-12 
  2008-01 
  2008-02 
  2008-03 
  2008-04 
  2008-05 
  2008-06 
  2008-07 
  2008-08 
  contact us 
  some jokes 
  song lyrics 
  public key 


  

  

hash tables compared

Recently I had a thought to see how different languages performed with hash table insertions.

The languages to test were c#, java, perl, python and ruby.

I predicted that perl would outperform the others, letting python take second place, c# as third and finally java (as java is slow with nearly everything). Interestingly, the results were not as I imagined. I'm quite inexperienced in ruby, so I didn't have any estimation of where that might come.

The code snippets that I used are as follows:

hash_test.pl

#!/usr/bin/perl

use strict;
use warnings;

my %test = ();
my %config = ();

my $time = time();
$config{"size"} = 10000000;

for( my $i=0, my $s = $config{"size"}; $i<$s ; $i++ ) {
	$test{$i} = "a";
}

print( "Insertion of ${config{'size'}} items took: " .
	( time() - $time ) . "seconds\n" );
hash_test.py
#!/usr/bin/python

import time

test = {}
config = {}
config["size"] = 10000000

t = time.time() 

for a in range(config["size"]):
	test["a"] = a

timetaken = time.time() - t
print 'Time taken: %d\n' % ( timetaken )
hash_test.cs
using System.IO;
using System.Collections;
using System;

class hash_test {

	private static Hashtable test = new Hashtable();

	public static void Main( string []args ) {
		Hashtable config = new Hashtable();
		
		config["size"] = 10000000;

		DateTime d = DateTime.Now;

		for( int i = 0, s = (int)config["size"] ; i<s ; i++ ) {
			test[i] = i;
		}

		Console.WriteLine( ( DateTime.Now - d ).Seconds );
	}
}
hash_test.java
import java.io.*;
import java.util.*;

public class hash_test {
	public static HashMap test = new HashMap();
	
	public static void main( String []args ) {
		HashMap config = new HashMap();
		config.put( "size", 10000000 );
		int s = (Integer)config.get("size");

		long t = (new Date()).getTime() / 1000;

		for( int i=0 ; i<s ; i++ ) {
			test.put(i,i);
		}

		System.out.println( "Time taken: " + ( 
			(new Date()).getTime() / 1000 - t ) );
	}
}
hash_test.rb
#!/usr/bin/ruby

test = {}
config = {}

config["size"] = 10000000

s = config["size"]
t = Time.new
for i in 1..s
	test[i] = i
end

i = Time.new.to_i - t.to_i

printf( "Time taken: %d\n" , i )

execution results

languageexecution timecode time
c#12.0349
java10.051killed
python5.6455
perl77.852killed
ruby17.24018


The results above that are shown as killed are where the interpreter/runtime attempted to allocate too many resources and the OS killed the application.

In the case of the perl interpreter, the program ran for around 24 seconds before other processes were beginning to be swapped, the system became unusable for another 50 seconds before the program was killed.

This is a huge surprise for me, I've worshiped the church of perl for a long time, and it's fulfilled my needs and problems very well.

Given that c# takes a lot longer to write, and is more of a pain in the ass, and much less portable than ruby, I'd recommend ruby above c# for doing hash intensive work. Given that python is a simple to work with, I'd recommend it above ruby. Given the apparent limits on perl hashes, I'd say unless you intend storing a HUGE amount of data in a hash, use perl.