From 7461ed5227e3002c4a6f74d458aa0255b7c1217d Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 26 Jan 2010 17:32:25 +0100 Subject: 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 --- src/corelib/tools/qstringlist.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) 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; -- cgit v0.12