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. Memory difference between QMap<MyInst,QColor> and QMap<MyInst,QString)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 5 Posters 6.8k 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.
  • Q Qt Enthusiast
    9 Aug 2016, 13:08

    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

    K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 9 Aug 2016, 13:27 last edited by
    #2

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

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Aug 2016, 13:30 last edited by
      #3

      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
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on 9 Aug 2016, 13:32 last edited by
        #4

        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

        M K 2 Replies Last reply 9 Aug 2016, 13:34
        0
        • Q Qt Enthusiast
          9 Aug 2016, 13:32

          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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 9 Aug 2016, 13:34 last edited by
          #5

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

          1 Reply Last reply
          0
          • Q Qt Enthusiast
            9 Aug 2016, 13:32

            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

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 9 Aug 2016, 13:40 last edited by
            #6

            @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

            M 1 Reply Last reply 9 Aug 2016, 13:42
            1
            • K kshegunov
              9 Aug 2016, 13:40

              @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 *)
              
              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Aug 2016, 13:42 last edited by
              #7

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

              K 1 Reply Last reply 9 Aug 2016, 13:46
              0
              • M mrjj
                9 Aug 2016, 13:42

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

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 9 Aug 2016, 13:46 last edited by kshegunov 8 Sept 2016, 21:47
                #8

                @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

                M Q 2 Replies Last reply 9 Aug 2016, 14:06
                0
                • K kshegunov
                  9 Aug 2016, 13:46

                  @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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 9 Aug 2016, 14:06 last edited by
                  #9

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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 9 Aug 2016, 21:40 last edited by
                    #10

                    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
                    0
                    • K kshegunov
                      9 Aug 2016, 13:46

                      @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

                      Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on 10 Aug 2016, 05:00 last edited by
                      #11

                      Sorry does that mean QString takes more memory that QColor

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        Qt Enthusiast
                        wrote on 10 Aug 2016, 06:03 last edited by
                        #12

                        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
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 10 Aug 2016, 06:37 last edited by
                          #13

                          hi
                          24 bytes it seems. :)

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            Qt Enthusiast
                            wrote on 10 Aug 2016, 07:27 last edited by
                            #14

                            so QString takes more memory that QColor ?

                            M K 2 Replies Last reply 10 Aug 2016, 07:33
                            0
                            • Q Qt Enthusiast
                              10 Aug 2016, 07:27

                              so QString takes more memory that QColor ?

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 10 Aug 2016, 07:33 last edited by
                              #15

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

                              1 Reply Last reply
                              0
                              • Q Qt Enthusiast
                                10 Aug 2016, 07:27

                                so QString takes more memory that QColor ?

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 10 Aug 2016, 11:37 last edited by
                                #16

                                @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

                                J 1 Reply Last reply 10 Aug 2016, 11:43
                                1
                                • K kshegunov
                                  10 Aug 2016, 11:37

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

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 10 Aug 2016, 11:43 last edited by
                                  #17

                                  @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

                                  M Q 2 Replies Last reply 10 Aug 2016, 11:57
                                  0
                                  • J jsulm
                                    10 Aug 2016, 11:43

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

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 10 Aug 2016, 11:57 last edited by
                                    #18

                                    @jsulm
                                    Following your thought of unsigned int

                                    Would
                                    QMap<MyInst,QRgb);

                                    be as good?

                                    It does seems
                                    QColor::QColor(QRgb color)

                                    will ignore alpha.

                                    J 1 Reply Last reply 10 Aug 2016, 11:58
                                    0
                                    • M mrjj
                                      10 Aug 2016, 11:57

                                      @jsulm
                                      Following your thought of unsigned int

                                      Would
                                      QMap<MyInst,QRgb);

                                      be as good?

                                      It does seems
                                      QColor::QColor(QRgb color)

                                      will ignore alpha.

                                      J Offline
                                      J Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 10 Aug 2016, 11:58 last edited by
                                      #19

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

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

                                      M 1 Reply Last reply 10 Aug 2016, 12:00
                                      0
                                      • J jsulm
                                        10 Aug 2016, 11:58

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

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 10 Aug 2016, 12:00 last edited by mrjj 8 Oct 2016, 12:02
                                        #20

                                        @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
                                        0
                                        • J jsulm
                                          10 Aug 2016, 11:43

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

                                          Q Offline
                                          Q Offline
                                          Qt Enthusiast
                                          wrote on 10 Aug 2016, 12:07 last edited by
                                          #21

                                          Can u tell me how to represent integer for colors because we can have millions of colors ?

                                          J 2 Replies Last reply 10 Aug 2016, 12:44
                                          0

                                          11/24

                                          10 Aug 2016, 05:00

                                          • Login

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