Memory difference between QMap<MyInst,QColor> and QMap<MyInst,QString)
-
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
-
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
@Qt-Enthusiast
Why are you asking? Usually you shouldn't care. -
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. -
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
-
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
@Qt-Enthusiast
yes a hex value should be less. maybe.
u can just test it with
sizeof -
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
@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 *)
-
@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 *)
@kshegunov
yes u are right ( as always)
Just meant for checking how much QColor will take. -
@kshegunov
yes u are right ( as always)
Just meant for checking how much QColor will take.@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 -
@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.