Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stringHomeTask;
- public class HomeTask5 {
- public static void main(String[] args) {
- for (int i = 10; i <= 100000; i *= 10) {
- long startString = System.nanoTime();
- concat(i);
- long stopString = System.nanoTime();
- long startBuilder = System.nanoTime();
- concatBuilder(i);
- long stopBuilder = System.nanoTime();
- long resultConcat = stopString - startString;
- long resultBuilder = stopBuilder - startBuilder;
- if (resultBuilder < resultConcat) {
- System.out.println("StringBuilder method combines a string of " + i + " characters to " + (resultConcat / resultBuilder)
- + " faster than the String method");
- } else if (resultConcat < resultBuilder) {
- System.out.println("String method combines a string of " + i + " characters to " + (resultBuilder / resultConcat)
- + " faster than the StringBuilder method");
- } else {
- System.out.println("These same methods");
- }
- }
- }
- public static String concat(int value) {
- String result = "";
- for (int i = 0; i < value; i++) {
- //result.concat(i+"".concat(" "));
- result = result + i + " ";
- }
- return result;
- }
- public static StringBuilder concatBuilder(int value) {
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < value; i++) {
- result.append(i + " ");
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement