SHARE
TWEET

Java - Speed-Test about printing to stdout.

a guest Sep 17th, 2013 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         public static void main(String[] args) {
  2.                 List<Long> times = new ArrayList<Long>();
  3.  
  4.                 int iterations = 100;
  5.                 int secondIterations = 25;
  6.  
  7.                 long start = System.currentTimeMillis();
  8.                 for (int count = 0; count < iterations; count++) {
  9.                         for (int secondCounter = 0; secondCounter < secondIterations; secondCounter++) {
  10.                                 System.out.print("*");
  11.                         }
  12.                         System.out.println();
  13.                 }
  14.                 times.add(System.currentTimeMillis() - start);
  15.  
  16.                 start = System.currentTimeMillis();
  17.                 for (int count = 0; count < iterations; count++) {
  18.                         String line = "";
  19.                         for (int secondCounter = 0; secondCounter < secondIterations; secondCounter++) {
  20.                                 line += "*";
  21.                         }
  22.                         System.out.println(line);
  23.                 }
  24.                 times.add(System.currentTimeMillis() - start);
  25.  
  26.                 start = System.currentTimeMillis();
  27.                 for (int count = 0; count < iterations; count++) {
  28.                         StringBuilder builder = new StringBuilder();
  29.                         for (int secondCounter = 0; secondCounter < secondIterations; secondCounter++) {
  30.                                 builder.append("*");
  31.                         }
  32.                         System.out.println(builder.toString());
  33.                 }
  34.                 times.add(System.currentTimeMillis() - start);
  35.  
  36.                 System.out.println("Direct: " + Long.toString(times.get(0)) + "ms");
  37.                 System.out.println("String: " + Long.toString(times.get(1)) + "ms");
  38.                 System.out.println("Stringbuilder: " + Long.toString(times.get(2)) + "ms");
  39.         }
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