A little help with the speed of my program.

Hello,

I'm working on an assignment that does basic compression/decompression of files. I have the decompression program working, however it runs about 10000 times slower than the example .class file. I am hoping that someone can help me make my program a little faster. The code/algorithm isn't that complicated, if you want to look at a description of the algorithm go to:

http://www.cs.utah.edu/classes/cs1410/Assignments/extracredit3/CompressionTutorial.html

I think my "main" problem is that I create an int[] of size 2048 so I can backtrack the maximum distance(according to the "Pattern Algorithm") away from the current position in the OutputStream. It will wrap around itself to avoid an "ArrayOutofBounds". Anyways, thanks for your help and time.

Here is my code:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

/**

* @author Collin

*

*/

publicclass JDecompress

{

public JDecompress()

{

}

publicstaticvoid main(String[] args)

{

for(int i = 0; i < args.length; i++)

{

int[] outputData =newint[2048];

if(!args[i].endsWith(".jz"))

{

System.out.println("The file: " + args[i] +" is not compressed, skipping.");

}

else

{

try

{

File inputFile =new File (args[i]);

File outputFile =new File (args[i].substring(0, args[i].lastIndexOf(".jz")));

FileInputStream in=new FileInputStream (inputFile);

FileOutputStream out =new FileOutputStream (outputFile);

int index = 0;

int data = 0;

boolean stop =false;

boolean unique=false;

boolean repeated=false;

boolean pattern=false;

while(!stop)

{

data = in.read();// Data will contain the first byte from the input file.

//It will be in the range 0...255 inclusive, or -1 if

//the end of the file has been reached.

stop= (data == -1);

unique= ((-1 < data) && (data < 64));

repeated = ((63 < data) && (data < 128));

pattern = ((127 < data) && (data < 256));

if(unique)

{

for(int j = 0; j < data + 1; j++)

{

int temp = in.read();

if(temp != -1)

{

outputData[index] = (temp);

out.write(temp);

index = (index + 1) % 2048;

}

else

{

//Error Message Needed

}

}

}

elseif(repeated)

{

int temp = in.read();

if(temp != -1)

{

for(int j = 0; j < (data % 64) + 2; j++)

{

outputData[index] = (temp);

out.write(temp);

index = (index + 1) % 2048;

}

}

else

{

//Error Message Needed

}

}

elseif(pattern)

{

int temp = in.read();

if(temp != -1)

{

int D = (data % 128)*16 + (temp / 16) + 2;

int N = (temp % 16) + 2;

for(int j = 0; j < N; j++)

{

outputData[index] = outputData[(index - D + 2048) % 2048];

out.write(outputData[index]);

index = (index + 1) % 2048;

}

}

else

{

//Error message needed.

}

}

}// End of loop.

in.close();

out.close();

}

catch (IOException e)

{

//TODO Error Message needed.

}

}

}

}

}

[6779 byte] By [Ennovaa] at [2007-11-27 2:35:24]
# 1

Replace FileInputStream in= new FileInputStream (inputFile);

withInputStream in = new BufferedInputStream(new FileInputStream(inputFile));

Similarly, replace FileOutputStream out = new FileOutputStream (outputFile);

withOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));

Something like this should really be IO-bound, so buffering your streams will almost certainly have more effect on speed than anything else.

If it's still not fast enough, you'll want to profile it. To do that I suggest running it as java -Xrunhprof:cpu=times JDecompress yourtestfile.jzThis produces output in java.hprof.txt or java.hprof which will tell you where the CPU is spending its time. If you need help understanding that file, don't post the entire file but do ask for an explanation.

YAT_Archivista at 2007-7-12 2:53:44 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks,That is exactly what the problem was.
Ennovaa at 2007-7-12 2:53:44 > top of Java-index,Java Essentials,New To Java...