Advertisement
FedchenkoIhor

concat

Sep 29th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. package stringHomeTask;
  2.  
  3. public class HomeTask5 {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.     for (int i = 10; i <= 100000; i *= 10) {
  8.  
  9.         long startString = System.nanoTime();
  10.         concat(i);
  11.         long stopString = System.nanoTime();
  12.  
  13.         long startBuilder = System.nanoTime();
  14.         concatBuilder(i);
  15.         long stopBuilder = System.nanoTime();
  16.  
  17.         long resultConcat = stopString - startString;
  18.         long resultBuilder = stopBuilder - startBuilder;
  19.  
  20.         if (resultBuilder < resultConcat) {
  21.         System.out.println("StringBuilder method combines a string of " + i + " characters to " + (resultConcat / resultBuilder)
  22.             + " faster than the String method");
  23.         } else if (resultConcat < resultBuilder) {
  24.         System.out.println("String method combines a string of " + i + " characters to " + (resultBuilder / resultConcat)
  25.             + " faster than the StringBuilder method");
  26.         } else {
  27.         System.out.println("These same methods");
  28.         }
  29.     }
  30.     }
  31.  
  32.     public static String concat(int value) {
  33.     String result = "";
  34.     for (int i = 0; i < value; i++) {
  35.         //result.concat(i+"".concat(" "));
  36.         result = result + i + " ";
  37.     }
  38.     return result;
  39.     }
  40.  
  41.     public static StringBuilder concatBuilder(int value) {
  42.     StringBuilder result = new StringBuilder();
  43.     for (int i = 0; i < value; i++) {
  44.         result.append(i + " ");
  45.     }
  46.     return result;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement