Skip to content

Commit

Permalink
Performance tweak.. thank you https://github.com/wlbaker !
Browse files Browse the repository at this point in the history
  • Loading branch information
erichVK5 committed Jun 25, 2016
1 parent b59eaae commit e21a986
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions SourceBuffer.java
Expand Up @@ -29,6 +29,8 @@


import java.io.*;
import java.util.Scanner;
import java.lang.StringBuffer;

public class SourceBuffer {

Expand All @@ -45,7 +47,7 @@ public SourceBuffer(String filename) {
int c;
char [] buffer = new char[1000];
int bufferIndex = 0;
//System.out.println("about to read bytes from file");
// System.out.println("about to read bytes from file");
while ((c = input.read()) != -1) {
if (bufferIndex == buffer.length) {
char [] newBuffer = new char[buffer.length*2];
Expand Down Expand Up @@ -85,7 +87,7 @@ public int read_next_bit() {
result = source_char & (1 << bit);
}
bit--;
//System.out.println("bit now: " + bit);
// System.out.println("bit now: " + bit);
// System.out.println("source_index now: " + source_index);
return result;
}
Expand Down Expand Up @@ -131,7 +133,9 @@ public String decode() {
NodeTree tree = new NodeTree();

int out_file_length = uncompressed_size();
String sb = "";
//String sb = "";
// immutable Strings replaced with more efficient string handling, suggested by wlbaker:
StringBuffer sb = new StringBuffer(out_file_length);
// System.out.println("About to enter decoding while loop...");
while (source_index < source_buffer.length && sb.length() != out_file_length) {
//System.out.println("Have entered decoding while loop...");
Expand All @@ -151,7 +155,10 @@ public String decode() {
}
// System.out.println("Node symbol: " + (char)(node.symbol));
// System.out.println("Node symbol as toString: " + node);
sb = sb + (char)(node.symbol);

sb.append((char)node.symbol); // more efficient string building, thanks wlbaker
//sb = sb + (char)(node.symbol);

// sb = sb + node;
// sb = sb + ((char)(node.symbol & 0xff));
// node.weight += 1;
Expand All @@ -162,7 +169,7 @@ public String decode() {
}
//source_buffer = null; // not needed for standalone utility
//is_filled = false;
return sb;
return sb.toString();
}

}

0 comments on commit e21a986

Please sign in to comment.