Here is my code:
public static String compress(final String input)
{
HashMap<String, Integer> codes = new HashMap<String, Integer>();
for (int i = 0; i < 256; i++)
{
codes.put((char) i + "", i);
}
StringBuilder outputString = new StringBuilder();
int max_code = 32767;
int next_code = 257;
String currentString = new String();
char c;
for (int i = 0; i < input.length(); i++)
{
c = input.charAt(i);
currentString = currentString + c;
if (!codes.containsKey(currentString))
{
if (next_code <= max_code)
{
codes.put(currentString, next_code++);
}
currentString = currentString.substring(0, currentString.length() - 1);
outputString.append(codes.get(currentString));
currentString = c + "";
}
}
outputString.append(codes.get(currentString));
return outputString.toString();
}
I have worked from the article: http://marknelson.us/2011/11/08/lzw-revisited/
I have read some articles stating that this method is naive and very slow: https://code.google.com/p/algorithms-and-datastructures-course/source/browse/trunk/AD_exercise_4/src/ad_exercise_4/controller/LZWNaive.java?r=38
How can I speed up the algorithm. At the moment it is taking 21 seconds to compress 3MB. Could somebody provide the pseudocode that I should be following to achieve quicker results. For example 1-2 seconds to compress 3MB.
I think the !HashMap.containsKey() is the line which costs an extreme amount of time. 16 out of the 21 seconds.
Regards.