Skip to content

Instantly share code, notes, and snippets.

@ambud
Created March 14, 2018 21:03
Show Gist options
  • Save ambud/0c0a4ec4f6af61af9ce68f53f0440ac4 to your computer and use it in GitHub Desktop.
Save ambud/0c0a4ec4f6af61af9ce68f53f0440ac4 to your computer and use it in GitHub Desktop.
package com.srotya.sidewinder.test;
import java.nio.charset.Charset;
public class SimpleStringCompression {
private static Charset UTF8 = Charset.forName("utf-8");
private static Charset UTF16 = Charset.forName("utf-16");
public static void main(String[] args) {
String asciiData = "dasdqwecddvadfsdfadasndueij2e0wehfunwefjqefh 83rhu n2f[0hf";
long ts = System.currentTimeMillis();
for (int i = 0; i < 10_000_000; i++) {
String compressedString = compressedString(asciiData);
}
ts = System.currentTimeMillis() - ts;
System.out.println(ts);
// System.out.println("After:"+compressedString.length()+"
// Before:"+asciiData.length());
// System.out.println(uncompressString(compressedString));
}
public static String uncompressString(String input) {
byte[] bytes = input.getBytes(UTF16);
return new String(bytes, UTF8);
}
public static String compressedString(String input) {
byte[] bytes = input.getBytes(UTF8);
return new String(bytes, UTF16);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment