Unsolved Creates a single QString from QStringList + join or by using QString::operator+=
-
Hello,
I have to build a QString from several strings. I was wondering :
"Is it better to use the provided operator+= in QString or to use QStringList to complete it and then join the result at the end ?"
Thank you by advance
-
@mistralegna
Is this a bottleneck for your application's performance? If not, my advice is use whatever is more convenient (I usually preferQString::join
if I have the strings in a list already).Kind regards.
-
You can insert all your strings into QStringList and then use join to get a single string. Inside join you can set delimiter.
For example:
StringList temp;
temp.append("a");
temp.append("b");
temp.append("c");
String new_string = temp.join("@");The result will be: a@b@c
-
Yes, I am aware of this possibility. The main question is: what are the performance of each way to doing this ?
Thank you for your answers!
-
I believe that join is O(1). With += you should have loop if there are many strings to concat.
-
@mistralegna You can write a simple benchmark to check what is faster in your case.
-
@mistralegna
Basically both methods should be the same, since you need to create a single string from a list of strings.
Still i would prefer QStringList::join() since (AFAIK) it should use QStringBuilder internally. The big advantage is that memory allocations are reduced to a minimum which results in a performance gain.Note: you can also use the (internal) StringBuilder by using the
%
operator instead the+
operator.