Hi!
I'm trying to transfer files and to do so must, of course, read them. Now, my reference File has a size of 4.240.821 Bytes and I need something between 9 and 14 seconds to simply read it and save the data to arrays.
When however I transfer the file via FTP, everything is done in about 1 second - reading, transferring and writing.
I've written the following code, to find out, how much time is needed with different sizes of arrays:
[CODE=java]
import java.io.IOExcep tion;
import java.io.FileInp utStream;
import java.io.File;
public class FileReadSpeed {
public static void main(String[] args) {
int howMany = 8;
int howOften = 5;
long[][] results = new long[howMany][howOften];
int size = 64;
try
{
File file = new File("E:\\temp\ \file.tar.gz");
for(int i=8;i<8+howMany ;i++)
{
size *= 2;
for(int j=0; j<howOften; j++)
{
System.gc();
FileInputStream fiStream = new FileInputStream (file);
long startTime = System.currentT imeMillis();
byte[] data = new byte[size];
long amount = file.length() / size;
if(file.length( ) % size > 0) amount++;
for(int k=0; k<amount; k++)
{
for(int l=0; l<data.length; l++)
{
if((k == amount-1) && (l == file.length() % size)) break;
data[l] = (byte) fiStream.read() ;
}
}
long finishTime = System.currentT imeMillis();
long timeTaken = finishTime - startTime;
results[i-8][j] = timeTaken;
System.out.prin tln("Recorded (" + (i-8) + " | " + j + ") with size = " + size + " Bytes");
}
}
}
catch(IOExcepti on ioe)
{
System.err.prin tln(ioe);
}
System.out.prin tln();
size = 64;
for(int i=0; i<results.lengt h; i++)
{
size *= 2;
System.out.prin t(size + ":\t");
for(int j=0; j<results[i].length; j++)
{
System.out.prin t(results[i][j] + "\t");
}
double tmp = 0.;
for(int j=0; j<results[i].length; j++)
{
tmp += results[i][j];
}
System.out.prin tln("-> " + tmp/results[0].length);
}
}
}
[/CODE]Does anyone know, if there are implementations of FileInputStream or similar, which are faster? (Maybe with reduced functionality?)
Greetings,
Nepomuk
I'm trying to transfer files and to do so must, of course, read them. Now, my reference File has a size of 4.240.821 Bytes and I need something between 9 and 14 seconds to simply read it and save the data to arrays.
When however I transfer the file via FTP, everything is done in about 1 second - reading, transferring and writing.
I've written the following code, to find out, how much time is needed with different sizes of arrays:
[CODE=java]
import java.io.IOExcep tion;
import java.io.FileInp utStream;
import java.io.File;
public class FileReadSpeed {
public static void main(String[] args) {
int howMany = 8;
int howOften = 5;
long[][] results = new long[howMany][howOften];
int size = 64;
try
{
File file = new File("E:\\temp\ \file.tar.gz");
for(int i=8;i<8+howMany ;i++)
{
size *= 2;
for(int j=0; j<howOften; j++)
{
System.gc();
FileInputStream fiStream = new FileInputStream (file);
long startTime = System.currentT imeMillis();
byte[] data = new byte[size];
long amount = file.length() / size;
if(file.length( ) % size > 0) amount++;
for(int k=0; k<amount; k++)
{
for(int l=0; l<data.length; l++)
{
if((k == amount-1) && (l == file.length() % size)) break;
data[l] = (byte) fiStream.read() ;
}
}
long finishTime = System.currentT imeMillis();
long timeTaken = finishTime - startTime;
results[i-8][j] = timeTaken;
System.out.prin tln("Recorded (" + (i-8) + " | " + j + ") with size = " + size + " Bytes");
}
}
}
catch(IOExcepti on ioe)
{
System.err.prin tln(ioe);
}
System.out.prin tln();
size = 64;
for(int i=0; i<results.lengt h; i++)
{
size *= 2;
System.out.prin t(size + ":\t");
for(int j=0; j<results[i].length; j++)
{
System.out.prin t(results[i][j] + "\t");
}
double tmp = 0.;
for(int j=0; j<results[i].length; j++)
{
tmp += results[i][j];
}
System.out.prin tln("-> " + tmp/results[0].length);
}
}
}
[/CODE]Does anyone know, if there are implementations of FileInputStream or similar, which are faster? (Maybe with reduced functionality?)
Greetings,
Nepomuk
Comment