Why when I fill the QVector as follows:
QVector< QPair<qint32, QString> > vector(10000000);
QString temp;
for (int i = 0; i < 10000000; ++i)
{
temp = QString::fromUtf8("Vasya");
vector.replace(i, qMakePair(i, temp));
}
my program uses 470 MB of RAM, and when this:
QVector< QPair<qint32, QString> > vector(10000000);
QString temp2 = "Vasya";
for (int i = 0; i < 10000000; ++i)
{
vector.replace(i, qMakePair(i, temp2));
}
it is only 90 MB of RAM?
Because internally QString is optimized to share memory of const objects. First case needs allocate memory each time when fromUtf8 invoked. On opposite second case always can reuse memory from existing const temp2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With