Creates a single QString from QStringList + join or by using QString::operator+=
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
Is this a bottleneck for your application's performance? If not, my advice is use whatever is more convenient (I usually prefer
QString::joinif I have the strings in a list already).
You can insert all your strings into QStringList and then use join to get a single string. Inside join you can set delimiter.
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.
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