diff options
author | Mitch Curtis <mitch.curtis@digia.com> | 2013-05-30 18:50:07 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-06-05 09:35:42 +0200 |
commit | 4f2c96eaa8bfa4d8a6dfb92096e4e4030d0cdea7 (patch) | |
tree | ba74302511acf81d48d13aad88c87e7d220f0d68 /src/corelib/tools/qset.h | |
parent | 80604a0786628a0c57eac7cc856720537956cc7f (diff) |
Iterate over the smaller set in QSet::intersect().
When calling intersect() on a large (1000000 items) QSet, with a small
(1000 items) QSet as the argument, the function takes signifcantly
longer than when the operand and the argument are reversed. This is
because the operand set is always iterated over in its entirety.
This patch changes intersect() to iterate over the smaller set. This
reduces the large operand scenario's benchmark to ~0.000063
milliseconds, compared to the current ~134 milliseconds:
1000000.intersect(1000) = empty: 0.000063 (was 134)
1000.intersect(1000000) = empty: 0.000039 (was 0.000036)
1000000.intersect(1000) = 500: 0.10 vs (was 130)
1000.intersect(1000000) = 500: 0.023 vs (was 0.093)
1000000.intersect(1000) = 1000: 0.20 vs (was 139)
1000.intersect(1000000) = 1000: 0.017 vs (was 0.016)
Task-number: QTBUG-22026
Change-Id: I54b25c49c78c458fef355e9c6222da8a64c7681f
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qset.h')
-rw-r--r-- | src/corelib/tools/qset.h | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index 1390c67c80..265861198d 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -254,8 +254,16 @@ Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other) template <class T> Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other) { - QSet<T> copy1(*this); - QSet<T> copy2(other); + QSet<T> copy1; + QSet<T> copy2; + if (size() <= other.size()) { + copy1 = *this; + copy2 = other; + } else { + copy1 = other; + copy2 = *this; + *this = copy1; + } typename QSet<T>::const_iterator i = copy1.constEnd(); while (i != copy1.constBegin()) { --i; |