JDK-4015161 : java.io.*Input* is too slow
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1996-11-17
  • Updated: 2013-11-01
  • Resolved: 2006-05-25
Related Reports
Duplicate :  
Description
Included are two similar programs in C and Java (/usr/green3/local.java/NIGHTLY1/jdk1.1/solaris/JDK1.1M-5).

The java version takes 443 times longer to run (far more than most comparisons)

The output of the two are:

ccode
ccount = 19990 took 210 milliseconds

java jcode
java count = 19990 took 93225 milliseconds


---------------------- The C code ---------------------------
#include <stdio.h>
main(int argc, char *argv[])
{
	char	buff[2000];
	FILE	*fd;
	int	count = 0;

	clock();
	fd = fopen("/usr/dist/share/lib/nameslistx", "r");
	while(fgets(buff, 2000, fd) != NULL)
	{
		count++;
	}
	printf("ccount = %d took %d milliseconds\n", count, clock()/1000);
}


--------------------- The Java Code -----------------------------------
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.Date;

class jcode
{
	public static void main(String arg[])
	{
		long start = new Date().getTime();
	    try {
		DataInputStream fs = new DataInputStream(new FileInputStream("/usr/dist/share/lib/nameslistx"));
		int count = 0;

		while(fs.available() > 0)
		{
			String line = fs.readLine();
			count++;
			//System.out.println("Line "+count+": "+line);
		}
		System.out.println("java count = "+count+" took "+(new Date().getTime() - start)+" milliseconds");
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
}

Comments
EVALUATION In Merlin (jdk1.4) JSR-51 introduced the java.nio and java.nio.channels packages. These add new APIs for scaleable I/O. In particular, if the example were to be re-written using a FileChannel and a direct byte buffer of reasonable size, you will see substantial performance improvements. Using these APIs, you can approach native performance more easily. That being said, the test provided would require some work to be usable as a true benchmark of steady-state Java performance for file reads. Many benchmarks, including the one provided fall into the category of "microbenchmarks". Benchmarks need to be written very carefully in order to avoid measuring JDK start-up time, VM byte-code compilation, garbage collection, code run in interpreted mode before compilation, etc. There are several sources on the web which discuss the pitfalls of microbenchmarks and provide tips on how to write them correctly. Closing this bug as a duplicate of 4313882 which covered the relevant portions of JSR-51.
25-05-2006

EVALUATION Yes, Java I/O could be faster. This performance gap has been addressed by the java.nio package. Also, the java.io package may be re-implemented on top of java.nio in a future release. ###@###.### 2002-05-07
07-05-2002