fork(8) download
  1.  
  2. class Main
  3. {
  4. public static void main (String[] args) throws java.lang.Exception
  5. {
  6. long start = System.nanoTime();
  7. for (int i = 0; i < 1E6; i++) {
  8. methodA((byte) i);
  9. }
  10. System.out.println("Method A took: " + (System.nanoTime() - start));
  11. start = System.nanoTime();
  12. for (int i = 0; i < 1E6; i++) {
  13. methodB((byte) i);
  14. }
  15. System.out.println("Method B took: " + (System.nanoTime() - start));
  16. }
  17.  
  18. public static String methodA(byte b) { // with String methods
  19. return String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
  20. }
  21.  
  22. public static String methodB(byte b) { // with bitwise
  23. return Integer.toBinaryString((b & 0xFF) + 0x100).substring(1);
  24. }
  25. }
Success #stdin #stdout 2.25s 380352KB
stdin
Standard input is empty
stdout
Method A took: 2033784663
Method B took: 161741141