简体   繁体   中英

Java fastest way to concatenate strings, integers and floats

What is the most performant way to build strings from strings, integers and floats? currently I'm 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 a way to do this faster?

That should already be fast - it'll use StringBuilder internally for concatenation. Arguably using StringBuilder explicitly could eliminate the concatenation of empty strings, but it's not likely to make a big difference.

How often are you doing this, anyway? It must be pretty often, for it to be a bottleneck... do you really need to do it that often?

EDIT: For those who are saying "Use StringBuilder, it'll be faster" - consider this code:

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);
    }
}

Compile that, then use javap -c to see what the compiler generates...

You could try using a StringBuilder .

(However, most Java compilers worth their salt will automatically optimize the code you've listed to use StringBuilder behind the scenes.)

使用StringBuilder

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

If you want it to go really fast you can try my library which allows you to log messages in under a micro-second without creating any garbage. https://github.com/peter-lawrey/Java-Chronicle

(As I say, it likely to be over the top for what you want)

concat3 method as below works fastest for me, performance for concat1 is jvm implementation/optimization dependent, it may perform better in other version of JVM but on my windows machine and remote linux red hat machine i tested on shows concat3 works the fastest ..

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;
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM