Linked by Thom Holwerda on Tue 12th Oct 2010 21:52 UTC
Permalink for comment 445031
To read all comments associated with this story, please click here.
To read all comments associated with this story, please click here.
Features
Linked by Thom Holwerda on 06/13/13 14:35 UTC
Linked by Thom Holwerda on 06/11/13 17:07 UTC
Linked by Thom Holwerda on 06/10/13 23:13 UTC
Linked by Thom Holwerda on 06/08/13 14:57 UTC
Linked by Thom Holwerda on 06/07/13 11:40 UTC
Linked by Thom Holwerda on 06/04/13 12:45 UTC
Linked by nfeske on 05/31/13 10:12 UTC
Linked by Thom Holwerda on 05/29/13 16:59 UTC
Linked by Thom Holwerda on 05/24/13 17:26 UTC
Linked by Thom Holwerda on 05/21/13 21:38 UTC
More Features »
Sponsored Links



Member since:
2007-04-20
This is a follow up on how fast Java is, compared to C. I did a quick benchmark - create hash table, insert 100K integers, lookup all those integers, then remove them from hash table.
One program is Java, using Java's HashMap. Another program is C, using my own hash table implementation. Both test were run on dual Pentium 3 machine, running NetBSD and native openjdk7-1.7.0.92.20100521nb1. Each program was run several times to warm up CPU cache and get average time values.
Java program time:
time /opt/pkg/java/openjdk7/bin/java -hotspot test
0.58 real 0.39 user 0.14 sys
Here is the time for C program:
p3smp$ time ./test.exe
0.08 real 0.05 user 0.02 sys
C program is about 7 times faster than Java.
Below is the source code for both programs:
import java.util.*;
import java.io.*;
public class test
{
public static void main(String args[])
{
/* Create hash table */
HashMap htab = new HashMap(100000, 0.75f);
/* Create array of integer objects */
Integer[] int_obj = new Integer[100000];
for(int i = 0; i < 100000; i++)
int_obj[i] = new Integer(i);
/* Hash table insert */
for(int i = 0; i < 100000; i++)
{
htab.put(int_obj[i], int_obj[i]);
}
/* Hash table lookup */
for(int i = 0; i < 100000; i++)
{
if(htab.get(int_obj[i]) == null)
{
System.out.println("Error: htab.get() returned null");
}
}
/* Hash table remove */
for(int i = 0; i < 100000; i++)
{
htab.remove(int_obj[i]);
}
}
}
#include "htab.h"
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
uint32_t *int_obj;
htab_t htab;
union htab_key key;
union htab_val val;
/* Create hash table */
if(htab_init(&htab, 100000, 0, 0, 0.75, HTAB_UINT32_CMP, NULL) != 0)
{
printf("Error: line=%d\n", __LINE__);
exit(1);
}
/* Create array of integers */
if((int_obj = malloc(100000 * sizeof(int))) == NULL)
{
printf("Error: line=%d\n", __LINE__);
exit(1);
}
for(i = 0; i < 100000; i++)
int_obj[i] = i;
/* Hash table insert */
for(i = 0; i < 100000; i++)
{
key.uint32_key = int_obj[i];
val.uint32_val = int_obj[i];
if(htab_insert(htab, &key, key.uint32_key, NULL, &val, 0) != 0)
{
printf("Error: line=%d\n", __LINE__);
exit(1);
}
}
/* Hash table lookup */
for(i = 0; i < 100000; i++)
{
key.uint32_key = int_obj[i];
if(htab_lookup(htab, &key, key.uint32_key, NULL, &val) != 0)
{
printf("Error: line=%d\n", __LINE__);
exit(1);
}
}
/* Hash table remove */
for(i = 0; i < 100000; i++)
{
key.uint32_key = int_obj[i];
if(htab_remove(htab, &key, key.uint32_key, NULL, NULL) != 0)
{
printf("Error: line=%d\n", __LINE__);
exit(1);
}
}
}