Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Suggest a faster way for replacing custom parameters in a string
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Suggest a faster way for replacing custom parameters in a string

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 4.2k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by T3STY
    #5

    That is what I was thinking to use. However, I was asking if there was anything that would replace all the parameters at once.
    Replacing strings so many times with QString::replace() causes a lot of memory reallocation in order to expand the string size to fit requirements, which also causes more CPU usage. While increasing the string size to twice every allocation is fast enough, doing so 3 or 4 times for 30 strings causes a huge performance loss.
    So, if there was any way to tell QString::replace() (or some other method) about more things to replace at once I think it would definitely be faster and cheaper for the memory.

    BTW, The format string could be made of even 20 parameters with extra (non parametric) words. Consider:

    "h: %h | hh: %hh | H: %h | HH: %HH | m: %m | mm: %mm | s: %s | ss: %ss | z: %z | zzz: %zzz | a: %a | A: %A |  | t: %t | e: %e | E: %E | ee: %ee | EE: %EE |  | %M"
    

    which includes only part of the parameters I want to implement. Also %M is a user message that could be even hundred of characters long. There is no typical string length that I could consider to reserve memory in advance.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #6

      I'd say sum up the total length of the parameters and call reserve on the string before doing the replacements.

      1 Reply Last reply
      1
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #7

        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...

        O 1 Reply Last reply
        0
        • T3STYT T3STY

          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...

          O Offline
          O Offline
          onek24
          wrote on last edited by
          #8

          @T3STY

          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, ...).

          1 Reply Last reply
          0
          • T3STYT Offline
            T3STYT Offline
            T3STY
            wrote on last edited by
            #9

            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.

            O 1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #10

              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".

              1 Reply Last reply
              0
              • T3STYT Offline
                T3STYT Offline
                T3STY
                wrote on last edited by
                #11

                @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 :)

                1 Reply Last reply
                0
                • T3STYT T3STY

                  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.

                  O Offline
                  O Offline
                  onek24
                  wrote on last edited by onek24
                  #12

                  @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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    What about just doing a good old

                    QString message = "{" QTime::currentTime().toString() + el_msg[] etc.
                    

                    ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • T3STYT Offline
                      T3STYT Offline
                      T3STY
                      wrote on last edited by
                      #14

                      @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 :)

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        If you want to be sure about the performance: QBENCHMARK ;)

                        Happy coding !

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved