Hello!
In copying some large data (lets say from an array) to a QVector, what should be faster?
Option 1:
for (int aaa = 0; aaa < SIZE_TO_COPY; aaa++)
{
myVector.append(myArray[aaa]);
}
for (int aaa = 0; aaa < SIZE_TO_COPY; aaa++)
{
myVector.append(myArray[aaa]);
}
To copy to clipboard, switch view to plain text mode
Option 2:
const int startPoint = myVector.size();
const int lastPos = startPoint + SIZE_TO_COPY;
myVector.resize(lastPos); //(myVector.size() + SIZE_TO_COPY)
for (int aaa = startPoint, bbb = 0; aaa < lastPos; aaa++, bbb++)
{
myVector.replace(aaa,myArray[bbb]);
}
const int startPoint = myVector.size();
const int lastPos = startPoint + SIZE_TO_COPY;
myVector.resize(lastPos); //(myVector.size() + SIZE_TO_COPY)
for (int aaa = startPoint, bbb = 0; aaa < lastPos; aaa++, bbb++)
{
myVector.replace(aaa,myArray[bbb]);
}
To copy to clipboard, switch view to plain text mode
Modifier 1: Include myVector.reserve(myVector.size() + SIZE_TO_COPY); before the loop in the first option or before the resize() in the second option.
Modifier 2: Use foreach(...) instead of for(...).
Thanks,
Momergil
Bookmarks