diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2010-01-26 16:32:25 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2010-01-26 16:41:21 (GMT) |
commit | 7461ed5227e3002c4a6f74d458aa0255b7c1217d (patch) | |
tree | a810eba3ef105ec451c254a92f8d5b1696738c6a /src | |
parent | 8de4f6f16ec2b5b3e92e25aafd2649f076c0b31b (diff) | |
download | Qt-7461ed5227e3002c4a6f74d458aa0255b7c1217d.zip Qt-7461ed5227e3002c4a6f74d458aa0255b7c1217d.tar.gz Qt-7461ed5227e3002c4a6f74d458aa0255b7c1217d.tar.bz2 |
Optimise QStringList::join by pre-allocating the final size.
This avoids a number of reallocations due to appending.
This patch was contributed to us.
Task-number: QTBUG-3242
Reviewed-by: Thiago Macieira
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qstringlist.cpp | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp index 4b0f488..e9bdf57 100644 --- a/src/corelib/tools/qstringlist.cpp +++ b/src/corelib/tools/qstringlist.cpp @@ -404,7 +404,17 @@ void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegExp &r */ QString QtPrivate::QStringList_join(const QStringList *that, const QString &sep) { + int totalLength = 0; + const int size = that->size(); + + for (int i = 0; i < size; ++i) + totalLength += that->at(i).size(); + + if(size > 0) + totalLength += sep.size() * (size - 1); + QString res; + res.reserve(totalLength); for (int i = 0; i < that->size(); ++i) { if (i) res += sep; |