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.javaimport 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
| language | execution time | code time |
|---|---|---|
| c# | 12.034 | 9 |
| java | 10.051 | killed |
| python | 5.645 | 5 |
| perl | 77.852 | killed |
| ruby | 17.240 | 18 |
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.

