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
StringBuilderinternally for concatenation. Arguably usingStringBuilderexplicitly 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:
Compile that, then use
javap -cto see what the compiler generates…