Java is the fastest way to concatenate strings, integers and floats

What is the most efficient way to create strings from strings, integers and floating point numbers? I am currently doing this and it uses a lot of CPU time.

String frame = this.frameTime + ":" +
    this.player.vertices[0].x + "," +
    this.player.vertices[0].y + "," +
    this.player.activeAnimId + "," +
    (int)this.player.virtualSpeed + "," +
    this.map.getCurrentTime() + 
    (this.player.frameSound == -1 ? "" : "," + this.player.frameSound) +
    (this.player.frameDecal.equals("") ? "" : "," + this.player.frameDecal) +
    ";";

Is there any way to do this faster?

+3
source share
5 answers

This should already be fast - it will use StringBuilderinternally for concatenation. Perhaps using StringBuilderexplicitly can eliminate the concatenation of empty strings, but this is unlikely to make much difference.

How often do you do this? This should be pretty common since it will be a bottleneck ... do you really need to do this often?

EDIT: , " StringBuilder, " - :

public class Test
{
    public static void main(String[] args)
    {
        int x = 10;
        int y = 20;
        int z = 30;
        String foo = x + "," + y + "," + z + ";";
        System.out.println(foo);
    }
}

, javap -c, , ...

+16

StringBuilder.

( Java, , , , StringBuilder .)

+1

StringBuilder.

String string = new StringBuilder("abcd").append(23).append(false).append("xyz").toString();
0

The concat3 method, as shown below, works faster for me, the performance for concat1 depends on the jvm implementation / optimization, it may work better in a different version of the JVM, but on my windows machine and the remote linux red hat machine I tested on, it shows that concat3 is faster ..

public class StringConcat {

public static void main(String[] args) {
    int run = 100 * 1000 * 1000;
    long startTime, total = 0;

    final String a = "aafswerg";
    final String b = "assdfsaf";
    final String c = "aasfasfsaf";
    final String d = "afafafdaa";
    final String e = "afdassadf";

    startTime = System.currentTimeMillis();
    concat1(run, a, b, c, d, e);
    total = System.currentTimeMillis() - startTime;
    System.out.println(total);

    startTime = System.currentTimeMillis();
    concat2(run, a, b, c, d, e);
    total = System.currentTimeMillis() - startTime;
    System.out.println(total);

    startTime = System.currentTimeMillis();
    concat3(run, a, b, c, d, e);
    total = System.currentTimeMillis() - startTime;
    System.out.println(total);
}

private static void concat3(int run, String a, String b, String c, String d, String e) {
    for (int i = 0; i < run; i++) {
        String str = new StringBuilder(a.length() + b.length() + c.length() + d.length() + e.length()).append(a)
                .append(b).append(c).append(d).append(e).toString();
    }
}

private static void concat2(int run, String a, String b, String c, String d, String e) {
    for (int i = 0; i < run; i++) {
        String str = new StringBuilder(a).append(b).append(c).append(d).append(e).toString();
    }
}

private static void concat1(int run, String a, String b, String c, String d, String e) {
    for (int i = 0; i < run; i++) {
        String str = a + b + c + d + e;
    }
}
}
-1
source

All Articles