Created
March 14, 2018 21:03
-
-
Save ambud/0c0a4ec4f6af61af9ce68f53f0440ac4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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