daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ConcatTimer {
  2.     public static void main(String[] args) {
  3.         final int iterations = 1000000;
  4.         long startTimeConcat = System.nanoTime();
  5.         for (int i = 0; i < iterations; i++) {
  6.             String myString = "a: " + Math.random() + ", b: " + Math.random() + ", c: " + Math.random();
  7.         }
  8.         long endTimeConcat = System.nanoTime();
  9.  
  10.         long startTimeSb = System.nanoTime();
  11.         for (int i = 0; i < iterations; i++) {
  12.             StringBuilder sb = new StringBuilder();
  13.             sb.append("a: ").append(Math.random())
  14.                     .append(", b: ").append(Math.random())
  15.                     .append(", c: ").append(Math.random());
  16.             String myString = sb.toString();
  17.         }
  18.         long endTimeSb = System.nanoTime();
  19.         System.out.println("Concat:        " + ((endTimeConcat - startTimeConcat) / (double)iterations));
  20.         System.out.println("StringBuilder: " + ((endTimeSb - startTimeSb) / (double)iterations));
  21.     }
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top