Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Memory difference between QMap<MyInst,QColor> and QMap<MyInst,QString)

    General and Desktop
    5
    24
    5290
    Loading More Posts
    • 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.
    • Q
      Qt Enthusiast last edited by

      I have two maps and maps can contains around 10000 entries

      What is better in terms of memory
      QMap<MyInst,QColor>

      and QMap<MyInst,QString)

      and how much memory penality is of one map over other

      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @Qt Enthusiast last edited by

        @Qt-Enthusiast
        Why are you asking? Usually you shouldn't care.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply Reply Quote 1
        • mrjj
          mrjj Lifetime Qt Champion last edited by

          also, what is in the QString ?
          QColor has fixed size but QString could be MB in size.
          so all depends on what u put in QString.

          1 Reply Last reply Reply Quote 0
          • Q
            Qt Enthusiast last edited by

            it has hex value for QColor , as I understand having QMap<MyInst,QString) will be more optimal than QMap<MyInst,QColor> if I store hexvalue of a QColor in QString

            mrjj kshegunov 2 Replies Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion @Qt Enthusiast last edited by

              @Qt-Enthusiast
              yes a hex value should be less. maybe.
              u can just test it with
              sizeof

              1 Reply Last reply Reply Quote 0
              • kshegunov
                kshegunov Moderators @Qt Enthusiast last edited by

                @Qt-Enthusiast
                No! 1 character in QString is 2 bytes (it uses unicode), so the 6 characters you need to store the color will be 12 bytes vs the 3 (or 4 bytes with alpha) you need for representing a color. But again, why do you care? Just use what makes sense - QColor for colors, QString for strings ...

                @mrjj
                sizeof isn't useful here:

                sizeof(QString) == sizeof(void *)
                

                Read and abide by the Qt Code of Conduct

                mrjj 1 Reply Last reply Reply Quote 1
                • mrjj
                  mrjj Lifetime Qt Champion @kshegunov last edited by

                  @kshegunov
                  yes u are right ( as always)
                  Just meant for checking how much QColor will take.

                  kshegunov 1 Reply Last reply Reply Quote 0
                  • kshegunov
                    kshegunov Moderators @mrjj last edited by kshegunov

                    @mrjj
                    It's easier to just check it up:
                    http://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qcolor.h#n227

                    1 enum which is the word's size + 5 shorts in the union.
                    For a 64 bit machine this is 6 shorts 5 shorts and an int64 or 12 bytes 18 bytes. So there's no difference.

                    EDIT:
                    My math skills apparently suck.
                    A QString represented color will require 12 bytes for the 6 characters + 8 bytes on a 64 bit OS for the pointer + 4 bytes for the atomic reference counter used in the implicit sharing, which totals to 24 bytes

                    Read and abide by the Qt Code of Conduct

                    mrjj Q 2 Replies Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @kshegunov last edited by

                      @kshegunov
                      also, unless he has a 10.000.000
                      it might not really matter :)

                      1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        Hi,

                        And to add my fellows, if you have a doubt: benchmark. That way you'll what suites your needs best.

                        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 Reply Quote 0
                        • Q
                          Qt Enthusiast @kshegunov last edited by

                          Sorry does that mean QString takes more memory that QColor

                          1 Reply Last reply Reply Quote 0
                          • Q
                            Qt Enthusiast last edited by

                            EDIT:
                            My math skills apparently suck.
                            A QString represented color will require 12 bytes for the 6 characters + 8 bytes on a 64 bit OS for the pointer + 4 bytes for the atomic reference counter used in the implicit sharing, which totals to 24 bytes

                            This is not clear to me how much memory QString takes

                            1 Reply Last reply Reply Quote 0
                            • mrjj
                              mrjj Lifetime Qt Champion last edited by

                              hi
                              24 bytes it seems. :)

                              1 Reply Last reply Reply Quote 0
                              • Q
                                Qt Enthusiast last edited by

                                so QString takes more memory that QColor ?

                                mrjj kshegunov 2 Replies Last reply Reply Quote 0
                                • mrjj
                                  mrjj Lifetime Qt Champion @Qt Enthusiast last edited by

                                  @Qt-Enthusiast
                                  yes. on 64 bit, even more.

                                  1 Reply Last reply Reply Quote 0
                                  • kshegunov
                                    kshegunov Moderators @Qt Enthusiast last edited by

                                    @Qt-Enthusiast
                                    18 bytes for a QColor instance vs about 30 bytes for a QString object with 6 letters (QString will also keep one zero character at the end and will store the length, as addition to the things I've described in my edit). So yes, QColor will take less memory.

                                    But I just can't stress this enough: use what makes sense. Even if QString were more memory efficient, the complications and the needed CPU time for conversions between colors and strings just makes it unsuitable. And small wonder, it's supposed to be a general purpose string, not a color. There's a separate class that represents colors, so you should use that in any case and not worry about how much bytes anything takes unless you hit the memory limit (which seems doubtful at this point).

                                    Read and abide by the Qt Code of Conduct

                                    jsulm 1 Reply Last reply Reply Quote 1
                                    • jsulm
                                      jsulm Lifetime Qt Champion @kshegunov last edited by

                                      @kshegunov Yes, using QString instead of QColor to represent a colour just to save some bytes is an example for how not to optimize software.
                                      @qtEnthusiast why not use an 32bit unsigned int to represent a colour? This way you only need 4 bytes.

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      mrjj Q 2 Replies Last reply Reply Quote 0
                                      • mrjj
                                        mrjj Lifetime Qt Champion @jsulm last edited by

                                        @jsulm
                                        Following your thought of unsigned int

                                        Would
                                        QMap<MyInst,QRgb);

                                        be as good?

                                        It does seems
                                        QColor::QColor(QRgb color)

                                        will ignore alpha.

                                        jsulm 1 Reply Last reply Reply Quote 0
                                        • jsulm
                                          jsulm Lifetime Qt Champion @mrjj last edited by

                                          @mrjj It depends on the requirements: is alpha needed?

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          mrjj 1 Reply Last reply Reply Quote 0
                                          • mrjj
                                            mrjj Lifetime Qt Champion @jsulm last edited by mrjj

                                            @jsulm
                                            Hi
                                            Not 100% sure but my guess would be
                                            yes from what he posted so far.

                                            update:
                                            funny enough it seems to like alpha the other way
                                            QRgb QColor::rgba() const

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post