Linked by Thom Holwerda on Tue 12th Oct 2010 21:52 UTC
Java "Oracle and IBM today announced that the companies will collaborate to allow developers and customers to build and innovate based on existing Java investments and the OpenJDK reference implementation. Specifically, the companies will collaborate in the OpenJDK community to develop the leading open source Java environment."
Permalink for comment 445031
To read all comments associated with this story, please click here.
RE[2]: Why all the fuss about Java?
by rom508 on Thu 14th Oct 2010 09:56 UTC in reply to "RE: Why all the fuss about Java?"
rom508
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);
}
}
}

Reply Parent Score: 1