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.
  • kshegunovK kshegunov

    @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 *)
    
    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #7

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

    kshegunovK 1 Reply Last reply
    0
    • mrjjM mrjj

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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #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

      mrjjM Q 2 Replies Last reply
      0
      • kshegunovK 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

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #9

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          • kshegunovK 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

            Q Offline
            Q Offline
            Qt Enthusiast
            wrote on 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 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
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #13

                hi
                24 bytes it seems. :)

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by
                  #14

                  so QString takes more memory that QColor ?

                  mrjjM kshegunovK 2 Replies Last reply
                  0
                  • Q Qt Enthusiast

                    so QString takes more memory that QColor ?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

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

                    1 Reply Last reply
                    0
                    • Q Qt Enthusiast

                      so QString takes more memory that QColor ?

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 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

                      jsulmJ 1 Reply Last reply
                      1
                      • kshegunovK kshegunov

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

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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

                        mrjjM Q 2 Replies Last reply
                        0
                        • jsulmJ jsulm

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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 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.

                          jsulmJ 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @jsulm
                            Following your thought of unsigned int

                            Would
                            QMap<MyInst,QRgb);

                            be as good?

                            It does seems
                            QColor::QColor(QRgb color)

                            will ignore alpha.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #19

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

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

                            mrjjM 1 Reply Last reply
                            0
                            • jsulmJ jsulm

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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #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
                              • jsulmJ jsulm

                                @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 last edited by
                                #21

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

                                jsulmJ 2 Replies Last reply
                                0
                                • Q Qt Enthusiast

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

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #22

                                  @Qt-Enthusiast 32bit integer can represent billions of values/colors.
                                  RGB - means 1byte for red, 1byte for green and one byte for blue. Since 32bit integer consists of 4 bytes you still have one byte for alpha channel.

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

                                  1 Reply Last reply
                                  1
                                  • Q Qt Enthusiast

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

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #23

                                    @Qt-Enthusiast See here: http://doc.qt.io/qt-5.7/qcolor.html#QRgb-typedef

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

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

                                      What exactly do you want to do with that map ?

                                      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