QList of at compared to [] billion times the speed

Copyright: https://blog.csdn.net/nicai_xiaoqinxi/article/details/91050289

To see who's faster speed?

surroundings

  • windows10 system
  • Qt4.8.7(gcc 4.9.2)
  • Qt5.12.3(gcc 7.3.0)
  • Qt Debug build
  • 1 billion operations compare

Compared

  • Unit ms
  • Finally, Appendix Source
Qt version reference at const at [] const []
4.8.7 14 278 279 639 629
5.12.3 14 325 322 418 411

analysis

  • Qt5.12.3 overall speed is faster than Qt4.8.7;
  • Whether or Qt5.12.3 Qt4.8.7 speed at the superior [];
  • and at [] or less the speed at Qt4.8.7 version;
  • In the version at Qt5.12.3 and [] speed or less.

appendix

  • Qt4.8.7 and Qt5.12.3 the same source QList
template <typename T>
inline const T &QList<T>::at(int i) const
{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");
 return reinterpret_cast<Node *>(p.at(i))->t(); }
 
template <typename T>
inline const T &QList<T>::operator[](int i) const
{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
 return reinterpret_cast<Node *>(p.at(i))->t(); }
 
template <typename T>
inline T &QList<T>::operator[](int i)
{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
  detach(); return reinterpret_cast<Node *>(p.at(i))->t(); }
  • Source Testing
#include <QList>
#include <QElapsedTimer>
#include <QUuid>
#include <QDebug>

#define ForLoop for (unsigned int i = 0; i < 10*1000*1000; i++)
static QList<int> list;
static QElapsedTimer timer;

qint64 runningTime()
{
    timer.start();

    ForLoop {
        int count = 1 + 2 + 3;
    }

    return timer.elapsed();
}

qint64 atRunningTime()
{
    timer.start();

    ForLoop {
        int count = list.at(0) + list.at(1) + list.at(2);
    }

    return timer.elapsed();
}

qint64 constAtRunningTime()
{
    timer.start();

    ForLoop {
        const int count = list.at(0) + list.at(1) + list.at(2);
    }

    return timer.elapsed();
}

qint64 squareOperationRunningTime()
{
    timer.start();

    ForLoop {
        int count = list[0] + list[1] + list[2];
    }

    return timer.elapsed();
}

qint64 constSquareOperationRunningTime()
{
    timer.start();

    ForLoop {
        const int count = list[0] + list[1] + list[2];
    }

    return timer.elapsed();
}

int main(int argc, char *argv[])
{
    list<<0<<1<<2;

    qDebug()<<"SourceRunningTime: "<<runningTime()<<"milliseconds.";
    qDebug()<<"atRunningTime: "<<atRunningTime()<<"milliseconds.";
    qDebug()<<"constAtRunningTime: "<<constAtRunningTime()<<"milliseconds.";
    qDebug()<<"squareOperationRunningTime: "<<squareOperationRunningTime()<<"milliseconds.";
    qDebug()<<"constSquareOperationRunningTime: "<<constSquareOperationRunningTime()<<"milliseconds.";

    return 0;
}

Guess you like

Origin blog.csdn.net/nicai_xiaoqinxi/article/details/91050289