Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

File processing : how could C# have better performance than Java ?

807591Feb 29 2008 — edited Mar 3 2008
Hi,

I've just made a test between Java and C#.
The test is simple :
- start timer
- read some text files
- end timer

I've run this test 5 times with 7 steps (0, 5,10, ..., 30 files).
To my own surprise : C# is 2 times more efficient than Java ...

Question : Is somebody have an explanation and maybe a way to improve Java performances ?_

I've already test to compile the Java program with an AOT : the perf' have been increased by only 10%. But that's not the good way and that's not a good result.

Here is my results :
nbFiles	Java-1	Java-2	Java-3	Java-4	Java-5	C#-1	C#-2	C#-3	C#-4	C#-5
0	0	0	0	0	0	0	0	0	0	0
5	47	31	47	47	31	15	31	15	15	15
10	79	93	78	79	78	46	31	46	31	31
15	109	141	109	141	109	62	62	46	62	62
20	156	156	157	156	156	78	62	78	78	78
25	203	188	203	203	297	109	93	93	93	93
30	234	235	234	312	235	109	109	109	125	109
Java - with sun jdk/jre 1.6 :
package javatestreader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class Main
{
    private static final File DIR = new File("C:\\Documents and Settings\\...\\data");

    public static void main(String[] args) throws IOException
    {
        long[][] results = new long[7][5];
        for (int nStep = 0; nStep <= 6; nStep++)
        {
            int nbFiles = nStep * 5;
            for (int nTest = 0; nTest < 5; nTest++)
            {
                Date start = new Date();
                readFiles(nbFiles);
                Date end = new Date();
                results[nStep][nTest] = end.getTime() - start.getTime();
            }
        }
        export(results);
    }

    private static void readFiles(int nbFiles) throws IOException
    {
        for (int nFile = 0; nFile < nbFiles; nFile++)
        {
            File file = new File(DIR, nFile + "-test-import.csv");
            BufferedReader reader = new BufferedReader(new FileReader(file));
            int nbLines = 0;
            while (reader.readLine() != null)
            {
                nbLines++;
            }
            reader.close();
        }
    }

    private static void export(long[][] results) throws IOException
    {
        PrintWriter writer = new PrintWriter("results.txt");
        for (long[] line : results)
        {
            for (long value : line)
            {
                writer.print(value);
                writer.print(",");
            }
            writer.println();
        }
        writer.close();
    }
}
C# - with Microsoft .Net 3.5 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace cs_test_reader
{
    class Program
    {
        private const String DIR = "C:\\Documents and Settings\\...\\data\\";

        static void Main(string[] args)
        {
            long[,] results = new long[7, 5];
            for (int nStep = 0; nStep <= 6; nStep++)
            {
                int nbFiles = nStep * 5;
                for (int nTest = 0; nTest < 5; nTest++)
                {
                    DateTime start = DateTime.Now;
                    readFiles(nbFiles);
                    DateTime end = DateTime.Now;
                    results[nStep,nTest] = end.Subtract(start).Milliseconds;
                }
            }
            export(results);
        }

        private static void readFiles(int nbFiles)
        {
            for (int nFile = 0; nFile < nbFiles; nFile++)
            {
                String file = DIR + nFile + "-test-import.csv";
                StreamReader reader = File.OpenText(file);
                int nbLines = 0;
                while (reader.ReadLine() != null)
                {
                    nbLines++;
                }
                reader.Close();
            }
        }

        private static void export(long[,] results)
        {
            StreamWriter writer = File.CreateText("results.txt");
            for (int i = 0; i < results.GetLength(0); i++)
            {
                for (int j = 0; j < results.GetLength(1); j++)
                {
                    writer.Write(results[i, j]);
                    writer.Write(",");
                }
                writer.WriteLine();
            }
            writer.Close();
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 31 2008
Added on Feb 29 2008
5 comments
374 views