[SOLVED] Suggest a faster way for replacing custom parameters in a string
-
I'd say sum up the total length of the parameters and call reserve on the string before doing the replacements.
-
That's a good idea! Thanks :)
But it will still not solve the number replacements I should make... I have to think about some other strategy... -
I can't provide any idea or solution, but maybe i can give you a hint - something i would try: Check out the source-code of the QString class and see what ::args(...) does. Maybe you can use Qt's code and implement the functionality to have different kinds of parameter-types(%s, %EE, ...).
-
Unfortunately, modifying Qt's source is not a viable option because of the license I'm using (LGPL). But I think I found some strategy to maintain some const-ness in the initial string length. I am using pre-formatted strings with "<FONT class="something">%1</FONT>" html tags, so most formatting is done with CSS ( QPlainTextEdit::document::setDefaultStyleSheet() ). This will reduce the number of reallocations (before replacement) to 1 (the %1 argument) with QString::arg(), and then I replace all data in the final string. I'm also thinking about other possible tricks like asking the time to QTime::getCurrentTime() with a separate string, so I would only replace time once instead of for each parameter.
-
But it will still not solve the number replacements I should make
You'll need to convert the numbers to strings anyway, so you can do that first and get the length of these strings.
The problem here is that numeric conversions in Qt like Qstring::number return a new string (thus allocate memory).
You could allocate a handful of QStrings once, reserve some space in them and then use QTextStream to make the conversions "in place". -
@Chris-Kawa The number to string conversion is done via QVariant::toString() anyway, so that is not a problem. And reserving space is not necessary anymore if I use the tricks above :)
-
@T3STY said:
Unfortunately, modifying Qt's source is not a viable option because of the license I'm using (LGPL).
No need to modify Qt's source. Just check what Qt does in such a case and develop your own algorithm/method.
-
What about just doing a good old
QString message = "{" QTime::currentTime().toString() + el_msg[] etc.
?
-
@SGaist that would make it hard to follow a pre-format string. Yes, it would be faster and, to some extent, easier. But definitely not the solution in my case.
Anyway, eventually I went for the HTML pre-formatted strings method that I wrote about in an earlier post. It's the easier and faster solution for me and seems to have the lightest impact on performance of all methods I have tried.
Thank you all for help :)
-
If you want to be sure about the performance: QBENCHMARK ;)
Happy coding !