Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java连接字符串、整数和浮点的最快方法_Java_String Concatenation - Fatal编程技术网

Java连接字符串、整数和浮点的最快方法

Java连接字符串、整数和浮点的最快方法,java,string-concatenation,Java,String Concatenation,从字符串、整数和浮点数构建字符串最有效的方法是什么?目前我正在做这件事,它占用了大量的cpu时间 String frame = this.frameTime + ":" + this.player.vertices[0].x + "," + this.player.vertices[0].y + "," + this.player.activeAnimId + "," + (int)this.player.virtualSpeed + "," + this

从字符串、整数和浮点数构建字符串最有效的方法是什么?目前我正在做这件事,它占用了大量的cpu时间

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) +
    ";";

有没有更快的方法?您可以尝试使用
StringBuilder


(不过,大多数值得一试的Java编译器都会自动优化您列出的代码,以便在幕后使用
StringBuilder

这应该已经很快了——它会在内部使用
StringBuilder
进行连接。可以说,显式使用
StringBuilder
可以消除空字符串的串联,但这不太可能产生很大的影响

你多久做一次?这一定是经常发生的,因为它是一个瓶颈。。。你真的需要经常这么做吗

编辑:对于那些说“使用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
查看编译器生成什么…

使用
StringBuilder

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

如果你想让它运行得很快,你可以试试我的程序库,它可以让你在一微秒之内登录邮件,而不会产生任何垃圾


(正如我所说的,它可能超出了您想要的范围)

下面的concat3方法对我来说工作最快,concat1的性能取决于jvm的实现/优化,它在其他版本的jvm中可能表现得更好,但在我测试的windows机器和远程linux red hat机器上显示,concat3工作最快

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;
    }
}
}
公共类StringConcat{
公共静态void main(字符串[]args){
int run=100*1000*1000;
长启动时间,总计=0;
最终字符串a=“aafswerg”;
最后一个字符串b=“assdfsaf”;
最终字符串c=“aasfasf”;
最后一个字符串d=“afdaa”;
最终字符串e=“afdassadf”;
startTime=System.currentTimeMillis();
concat1(运行、a、b、c、d、e);
总计=System.currentTimeMillis()-startTime;
系统输出打印项次(总计);
startTime=System.currentTimeMillis();
concat2(运行、a、b、c、d、e);
总计=System.currentTimeMillis()-startTime;
系统输出打印项次(总计);
startTime=System.currentTimeMillis();
concat3(运行、a、b、c、d、e);
总计=System.currentTimeMillis()-startTime;
系统输出打印项次(总计);
}
私有静态void concat3(int-run、字符串a、字符串b、字符串c、字符串d、字符串e){
for(int i=0;i
你试过StringBuilder吗?你试过
String.format()
?@BrianHoover这使用StringBuilder。请参阅本文的解释。出于兴趣,你为什么认为这会更快?这也是我的想法-尽管我不清楚在什么情况下会发生这种情况。是在循环中使用+运算符时编译器不会转换吗?您可以使用您知道足够的容量初始化生成器(但如此短的字符串不会获得太多)。还有,为什么你需要这个,日志记录?@zolex:你真的需要在每一帧上都这样做吗?真正地这对谁有好处?@JavaKungFu:这不一定是一个循环。基本上是在使用字符串串联表达式的结果时,如果使用
stringfoo=x+“,”;foo=foo+z;foo=foo+“,”等,这将是浪费。@zolex:Eek,您正在使用默认编码调用
getBytes()
。请不要那样做。如果确实需要字符串表示,请使用
OutputStreamWriter
。但是为什么不使用
DataOutputStream
只写您想要记录的二进制数据呢?不需要字符串转换。这个程序在实践中显示哪种方法最快。并且可以在运行时进行测试。请让我知道为什么投票失败。