Memory difference between QMap<MyInst,QColor> and QMap<MyInst,QString)
-
@mrjj
It's easier to just check it up:
http://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qcolor.h#n2271 enum which is the word's size + 5 shorts in the union.
For a 64 bit machine this is6 shorts5 shorts and an int64 or12 bytes18 bytes. So there's no difference.EDIT:
My math skills apparently suck.
AQString
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@kshegunov
also, unless he has a 10.000.000
it might not really matter :) -
Hi,
And to add my fellows, if you have a doubt: benchmark. That way you'll what suites your needs best.
-
@mrjj
It's easier to just check it up:
http://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qcolor.h#n2271 enum which is the word's size + 5 shorts in the union.
For a 64 bit machine this is6 shorts5 shorts and an int64 or12 bytes18 bytes. So there's no difference.EDIT:
My math skills apparently suck.
AQString
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 bytesSorry does that mean QString takes more memory that QColor
-
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 bytesThis is not clear to me how much memory QString takes
-
hi
24 bytes it seems. :) -
so QString takes more memory that QColor ?
-
so QString takes more memory that QColor ?
@Qt-Enthusiast
yes. on 64 bit, even more. -
so QString takes more memory that QColor ?
@Qt-Enthusiast
18 bytes for aQColor
instance vs about 30 bytes for aQString
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). -
@Qt-Enthusiast
18 bytes for aQColor
instance vs about 30 bytes for aQString
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).@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. -
@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. -
@jsulm
Following your thought of unsigned intWould
QMap<MyInst,QRgb);be as good?
It does seems
QColor::QColor(QRgb color)will ignore alpha.
-
@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.Can u tell me how to represent integer for colors because we can have millions of colors ?
-
Can u tell me how to represent integer for colors because we can have millions of colors ?
@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. -
Can u tell me how to represent integer for colors because we can have millions of colors ?
-
What exactly do you want to do with that map ?