简体   繁体   English

在Java中附加字符串与附加字符

[英]Appending Strings vs appending chars in Java

I am trying to solve an algorithmic task where speed is of primary importance. 我正在尝试解决速度至关重要的算法任务。 In the algorithm, I am using a DFS search in a graph and in every step, I add a char and a String. 在算法中,我在图形中使用DFS搜索,并且在每个步骤中,我都添加了一个char和一个String。 I am not sure whether this is the bottleneck of my algorithm (probably not) but I am curious what is the fastest and most efficient way to do this. 我不确定这是否是我算法的瓶颈(可能不是),但是我很好奇这是最快,最有效的方法。

At the moment, I use this: 此刻,我使用这个:

transPred.symbol + word

I think that there is might be a better alternative than the "+" operator but most String methods only work with other Strings (would converting my char into String and using one of them make a difference?). 我认为可能有比“ +”运算符更好的替代方法,但是大多数String方法只能与其他Strings一起使用(将char转换为String并使用其中之一会有所作为吗?)。

Thanks for answers. 感谢您的回答。

EDIT: 编辑:

for (Transition transPred : state.transtitionsPred) {
            walk(someParameters, transPred.symbol + word);
        }

transPred.symbol is a char and word is a string transPred.symbol是一个字符, word是一个字符串

A very common problem / concern. 一个非常普遍的问题/关注。

Bear in mind that each String in java is immutable. 请记住,java中的每个String都是不可变的。 Thus, if you modify the string it actually creates a new object. 因此,如果您修改字符串,它实际上会创建一个新对象。 This results in one new object for each concatenation you're doing above. 这样,您在上面进行的每个串联操作都会产生一个新对象。 This isn't great, as it's simply creating garbage that will have to be collected at some point. 这不是很好,因为它只是在某些时候创建必须收集的垃圾。

If your graph is overly large, this might be during your traversal logic - and it may slow down your algorithm. 如果您的图形太大,则可能是在遍历逻辑期间-可能会减慢算法的速度。

To avoid creating a new String for each concatenation, use the StringBuilder . 为了避免为每个串联创建新的String,请使用StringBuilder You can declare one outside your loop and then append each character with StringBuilder.append(char) . 您可以在循环外声明一个,然后在每个字符后附加StringBuilder.append(char) This does not incur a new object creation for each append() operation. 这不会为每个append()操作创建一个新的对象。

After your loop you can use StringBuilder.toString() , this will create a new object (the String) but it will only be one for your entire loop. 循环后,您可以使用StringBuilder.toString() ,这创建一个新对象(字符串),但在整个循环中仅是一个对象。

Since you replace one char in the string at each iteration I don't think that there is anything faster than a simple + append operation. 由于您在每次迭代中都在字符串中替换了一个char,所以我认为没有什么比简单的+ append操作更快的了。 As mentioned, Strings are immutable, so when you append a char to it, you will get a new String object, but this seems to be unavoidable in your case since you need a new string at each iteration. 如前所述,字符串是不可变的,因此在向其添加字符时,将获得一个新的String对象,但是在您的情况下,这似乎是不可避免的,因为每次迭代都需要一个新的字符串。

If you really want to optimize this part, consider using something mutable like an array of chars. 如果您确实要优化此部分,请考虑使用可变变量,例如字符数组。 This would allow you to replace the first character without any excessive object creation. 这样,您就可以替换第一个字符,而无需创建过多的对象。

Also, I think you're right when you say that this probably isn't your bottleneck. 另外,当您说这可能不是瓶颈时,我认为您是对的。 And remember that premature optimization is the root of all evil etc. (Don't mind the irony that the most popular example of good optimization is avoiding excessive string concatenation). 并记住,过早的优化是万恶之源 。(不要讽刺,良好优化的最流行示例是避免过多的字符串连接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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