Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Created August 22, 2014 15:42
Show Gist options
  • Save TrevorS/4731c39d21f50c2215f1 to your computer and use it in GitHub Desktop.
Save TrevorS/4731c39d21f50c2215f1 to your computer and use it in GitHub Desktop.
public class ChangeBase {
public static void main(String[] args) {
short[][] shorts = { { 0x00, 0x28 }, { 0x10, 0x28 }, { 0x1F, 0x9C } };
int iterations = 102533960;
long start = System.nanoTime();
for (int i = 0; i < iterations; i++) {
for (short[] s : shorts) {
convertToIntegerUsingStringManipulation(s);
}
}
long durationS = System.nanoTime() - start;
start = System.nanoTime();
for (int i = 0; i < iterations; i++) {
for (short[] s : shorts) {
convertToIntegerUsingBitManipulation(s);
}
}
long durationB = System.nanoTime() - start;
System.out.println("durationS: " + durationS / 1000000000.0);
System.out.println("durationB: " + durationB / 1000000000.0);
System.out.println("durationS / durationB: " + (durationS / durationB));
}
private static int convertToIntegerUsingBitManipulation(short[] shorts) {
return (shorts[0] << 8 | shorts[1] & 0xFF);
}
private static int convertToIntegerUsingStringManipulation(short[] shorts) {
return Integer.parseInt(
Integer.toBinaryString(shorts[0]) + String.format("%8s",
Integer.toBinaryString(shorts[1])).replace(' ', '0'), 2);
}
}
@TrevorS
Copy link
Author

TrevorS commented Aug 22, 2014

durationS: 274.23779
durationB: 1.255262
durationS / durationB: 218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment