diff options
Diffstat (limited to 'tests/benchmarks/corelib/tools')
-rw-r--r-- | tests/benchmarks/corelib/tools/qmap/main.cpp | 165 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qmap/qmap.pro | 5 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qstringlist/main.cpp | 4 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qvector/qrawvector.h | 66 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/tools.pro | 1 |
5 files changed, 199 insertions, 42 deletions
diff --git a/tests/benchmarks/corelib/tools/qmap/main.cpp b/tests/benchmarks/corelib/tools/qmap/main.cpp new file mode 100644 index 0000000000..e68ea685a3 --- /dev/null +++ b/tests/benchmarks/corelib/tools/qmap/main.cpp @@ -0,0 +1,165 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QFile> +#include <QMap> +#include <QString> +#include <QTest> +#include <qdebug.h> + + +class tst_QMap : public QObject +{ + Q_OBJECT + +private slots: + void insertion_int_int(); + void insertion_int_string(); + void insertion_string_int(); + + void lookup_int_int(); + void lookup_int_string(); + void lookup_string_int(); + + void iteration(); +}; + + +void tst_QMap::insertion_int_int() +{ + QMap<int, int> map; + QBENCHMARK { + for (int i = 0; i < 100000; ++i) + map.insert(i, i); + } +} + +void tst_QMap::insertion_int_string() +{ + QMap<int, QString> map; + QString str("Hello World"); + QBENCHMARK { + for (int i = 0; i < 100000; ++i) + map.insert(i, str); + } +} + +void tst_QMap::insertion_string_int() +{ + QMap<QString, int> map; + QString str("Hello World"); + QBENCHMARK { + for (int i = 1; i < 100000; ++i) { + str[0] = QChar(i); + map.insert(str, i); + } + } +} + + +void tst_QMap::lookup_int_int() +{ + QMap<int, int> map; + for (int i = 0; i < 100000; ++i) + map.insert(i, i); + + int sum = 0; + QBENCHMARK { + for (int i = 0; i < 100000; ++i) + sum += map.value(i); + } +} + +void tst_QMap::lookup_int_string() +{ + QMap<int, QString> map; + QString str("Hello World"); + for (int i = 0; i < 100000; ++i) + map.insert(i, str); + + QBENCHMARK { + for (int i = 0; i < 100000; ++i) + str += map.value(i); + } +} + +void tst_QMap::lookup_string_int() +{ + QMap<QString, int> map; + QString str("Hello World"); + for (int i = 1; i < 100000; ++i) { + str[0] = QChar(i); + map.insert(str, i); + } + + int sum = 0; + QBENCHMARK { + for (int i = 1; i < 100000; ++i) { + str[0] = QChar(i); + sum += map.value(str); + } + } +} + +// iteration speed doesn't depend on the type of the map. +void tst_QMap::iteration() +{ + QMap<int, int> map; + for (int i = 0; i < 100000; ++i) + map.insert(i, i); + + int j = 0; + QBENCHMARK { + for (int i = 0; i < 100; ++i) { + QMap<int, int>::const_iterator it = map.constBegin(); + QMap<int, int>::const_iterator end = map.constEnd(); + while (it != end) { + j += *it; + ++it; + } + } + } +} + + +QTEST_MAIN(tst_QMap) + +#include "main.moc" diff --git a/tests/benchmarks/corelib/tools/qmap/qmap.pro b/tests/benchmarks/corelib/tools/qmap/qmap.pro new file mode 100644 index 0000000000..6a0c8d62bd --- /dev/null +++ b/tests/benchmarks/corelib/tools/qmap/qmap.pro @@ -0,0 +1,5 @@ +TARGET = tst_qmap +QT = core testlib +INCLUDEPATH += . +SOURCES += main.cpp +CONFIG += release diff --git a/tests/benchmarks/corelib/tools/qstringlist/main.cpp b/tests/benchmarks/corelib/tools/qstringlist/main.cpp index 48bf7f1fc5..b4c1be29d9 100644 --- a/tests/benchmarks/corelib/tools/qstringlist/main.cpp +++ b/tests/benchmarks/corelib/tools/qstringlist/main.cpp @@ -163,7 +163,6 @@ void tst_QStringList::split_qlist_qstring() const void tst_QStringList::split_stdvector_stdstring() const { -#ifndef QT_NO_STL QFETCH(QString, input); const char split_char = ':'; std::string stdinput = input.toStdString(); @@ -176,12 +175,10 @@ void tst_QStringList::split_stdvector_stdstring() const token.push_back(each)) ; } -#endif } void tst_QStringList::split_stdvector_stdwstring() const { -#ifndef QT_NO_STL QFETCH(QString, input); const wchar_t split_char = ':'; std::wstring stdinput = input.toStdWString(); @@ -194,7 +191,6 @@ void tst_QStringList::split_stdvector_stdwstring() const token.push_back(each)) ; } -#endif } void tst_QStringList::split_stdlist_stdstring() const diff --git a/tests/benchmarks/corelib/tools/qvector/qrawvector.h b/tests/benchmarks/corelib/tools/qvector/qrawvector.h index 7e570d93ff..18d9847c95 100644 --- a/tests/benchmarks/corelib/tools/qvector/qrawvector.h +++ b/tests/benchmarks/corelib/tools/qvector/qrawvector.h @@ -48,10 +48,8 @@ #include <QtCore/qalgorithms.h> #include <QtCore/qlist.h> -#ifndef QT_NO_STL #include <iterator> #include <vector> -#endif #include <stddef.h> #include <stdlib.h> #include <string.h> @@ -66,24 +64,18 @@ QT_BEGIN_NAMESPACE template <typename T> class QRawVector { - struct Data : QVectorData { T array[1]; }; + typedef QVectorTypedData<T> Data; T *m_begin; int m_size; int m_alloc; public: - //static Data dummy; - //int headerOffset() { return (char*)&dummy.array - (char*)&dummy; } - inline int headerOffset() const { - // gcc complains about: return offsetof(Data, array); and also - // does not like '0' in the expression below. - return (char *)&(((Data *)(1))->array) - (char *)1; - } - inline Data *toBase(T *begin) const - { return (Data*)((char*)begin - headerOffset()); } - inline T *fromBase(void *d) const - { return (T*)((char*)d + headerOffset()); } + static Data *toBase(T *begin) + { return (Data*)((char*)begin - offsetOfTypedData()); } + static T *fromBase(void *d) + { return (T*)((char*)d + offsetOfTypedData()); } + inline QRawVector() { m_begin = fromBase(0); m_alloc = m_size = 0; realloc(m_size, m_alloc, true); } explicit QRawVector(int size); @@ -259,28 +251,29 @@ public: //static QRawVector<T> fromList(const QList<T> &list); -#ifndef QT_NO_STL static inline QRawVector<T> fromStdVector(const std::vector<T> &vector) { QRawVector<T> tmp; qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; } inline std::vector<T> toStdVector() const { std::vector<T> tmp; qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; } -#endif private: T *allocate(int alloc); void realloc(int size, int alloc, bool ref); void free(T *begin, int size); - int sizeOfTypedData() { - // this is more or less the same as sizeof(Data), except that it doesn't - // count the padding at the end - return reinterpret_cast<const char *>(&(reinterpret_cast<const Data *>(this))->array[1]) - reinterpret_cast<const char *>(this); + + class AlignmentDummy { QVectorData header; T array[1]; }; + + static Q_DECL_CONSTEXPR int offsetOfTypedData() + { + // (non-POD)-safe offsetof(AlignmentDummy, array) + return (sizeof(QVectorData) + (alignOfTypedData() - 1)) & ~(alignOfTypedData() - 1); } - static inline int alignOfTypedData() + static Q_DECL_CONSTEXPR int alignOfTypedData() { #ifdef Q_ALIGNOF - return qMax<int>(sizeof(void*), Q_ALIGNOF(Data)); + return Q_ALIGNOF(AlignmentDummy); #else - return 0; + return sizeof(void *); #endif } @@ -288,11 +281,11 @@ public: QVector<T> mutateToVector() { Data *d = toBase(m_begin); - d->ref = 1; + d->ref.initializeOwned(); d->alloc = m_alloc; d->size = m_size; - d->sharable = 0; - d->capacity = 0; + d->capacityReserved = 0; + d->offset = offsetOfTypedData(); QVector<T> v; *reinterpret_cast<QVectorData **>(&v) = d; @@ -309,7 +302,7 @@ void QRawVector<T>::reserve(int asize) template <typename T> void QRawVector<T>::resize(int asize) { realloc(asize, (asize > m_alloc || (asize < m_size && asize < (m_alloc >> 1))) - ? QVectorData::grow(sizeOfTypedData(), asize, sizeof(T), QTypeInfo<T>::isStatic) + ? QVectorData::grow(offsetOfTypedData(), asize, sizeof(T)) : m_alloc, false); } template <typename T> inline void QRawVector<T>::clear() @@ -370,7 +363,7 @@ QRawVector<T> &QRawVector<T>::operator=(const QRawVector<T> &v) template <typename T> inline T *QRawVector<T>::allocate(int aalloc) { - QVectorData *d = QVectorData::allocate(sizeOfTypedData() + (aalloc - 1) * sizeof(T), alignOfTypedData()); + QVectorData *d = QVectorData::allocate(offsetOfTypedData() + aalloc * sizeof(T), alignOfTypedData()); Q_CHECK_PTR(d); return fromBase(d); } @@ -386,7 +379,7 @@ QRawVector<T>::QRawVector(int asize) while (i != b) new (--i) T; } else { - qMemSet(m_begin, 0, asize * sizeof(T)); + memset(m_begin, 0, asize * sizeof(T)); } } @@ -446,10 +439,9 @@ void QRawVector<T>::realloc(int asize, int aalloc, bool ref) changed = true; } else { QT_TRY { - QVectorData *mem = QVectorData::reallocate( - toBase(m_begin), sizeOfTypedData() + (aalloc - 1) * sizeof(T), - sizeOfTypedData() -+ (xalloc - 1) * sizeof(T), alignOfTypedData()); + QVectorData *mem = QVectorData::reallocate(toBase(m_begin), + offsetOfTypedData() + aalloc * sizeof(T), + offsetOfTypedData() + xalloc * sizeof(T), alignOfTypedData()); Q_CHECK_PTR(mem); xbegin = fromBase(mem); xsize = m_size; @@ -482,7 +474,7 @@ void QRawVector<T>::realloc(int asize, int aalloc, bool ref) } else if (asize > xsize) { // initialize newly allocated memory to 0 - qMemSet(xbegin + xsize, 0, (asize - xsize) * sizeof(T)); + memset(xbegin + xsize, 0, (asize - xsize) * sizeof(T)); } xsize = asize; @@ -511,8 +503,7 @@ void QRawVector<T>::append(const T &t) { if (m_size + 1 > m_alloc) { const T copy(t); - realloc(m_size, QVectorData::grow(sizeOfTypedData(), m_size + 1, sizeof(T), - QTypeInfo<T>::isStatic), false); + realloc(m_size, QVectorData::grow(offsetOfTypedData(), m_size + 1, sizeof(T)), false); if (QTypeInfo<T>::isComplex) new (m_begin + m_size) T(copy); else @@ -533,8 +524,7 @@ typename QRawVector<T>::iterator QRawVector<T>::insert(iterator before, size_typ if (n != 0) { const T copy(t); if (m_size + n > m_alloc) - realloc(m_size, QVectorData::grow(sizeOfTypedData(), m_size + n, sizeof(T), - QTypeInfo<T>::isStatic), false); + realloc(m_size, QVectorData::grow(offsetOfTypedData(), m_size + n, sizeof(T)), false); if (QTypeInfo<T>::isStatic) { T *b = m_begin + m_size; T *i = m_begin + m_size + n; diff --git a/tests/benchmarks/corelib/tools/tools.pro b/tests/benchmarks/corelib/tools/tools.pro index ea9059e759..7565b1a167 100644 --- a/tests/benchmarks/corelib/tools/tools.pro +++ b/tests/benchmarks/corelib/tools/tools.pro @@ -5,6 +5,7 @@ SUBDIRS = \ qbytearray \ qcontiguouscache \ qlist \ + qmap \ qrect \ qregexp \ qstring \ |