Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConcatTimer {
- public static void main(String[] args) {
- final int iterations = 1000000;
- long startTimeConcat = System.nanoTime();
- for (int i = 0; i < iterations; i++) {
- String myString = "a: " + Math.random() + ", b: " + Math.random() + ", c: " + Math.random();
- }
- long endTimeConcat = System.nanoTime();
- long startTimeSb = System.nanoTime();
- for (int i = 0; i < iterations; i++) {
- StringBuilder sb = new StringBuilder();
- sb.append("a: ").append(Math.random())
- .append(", b: ").append(Math.random())
- .append(", c: ").append(Math.random());
- String myString = sb.toString();
- }
- long endTimeSb = System.nanoTime();
- System.out.println("Concat: " + ((endTimeConcat - startTimeConcat) / (double)iterations));
- System.out.println("StringBuilder: " + ((endTimeSb - startTimeSb) / (double)iterations));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement