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
    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
                • Q Qt Enthusiast
                  10 Aug 2016, 12:07

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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 10 Aug 2016, 12:44 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
                    10 Aug 2016, 12:07

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

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 10 Aug 2016, 12:49 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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 10 Aug 2016, 21:22 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

                      24/24

                      10 Aug 2016, 21:22

                      • Login

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