Created
August 22, 2014 15:42
-
-
Save TrevorS/4731c39d21f50c2215f1 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
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); | |
} | |
} |
Author
TrevorS
commented
Aug 22, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment