I decided to run very simple test to have glance on performance of Qt models and overhead cost of using them.
Something very simple as make 1M random strings with random size (note: rnd_str(int) return some random string with specified size)
int n = 1000000; QStandardItemModel std(n, 1); for(int row =0; row <n; ++row) std.setData(std.index(row,0), rnd_str(rand() % 16));
versus just vector of strings
QVector<QString> vec; for(int i =0; i< n; ++i) vec.push_back(rnd_str(rand() % 16));
Then just compare time of iterating through them:
QTime t = QTime::currentTime(); for(int row =0; row< n; ++row) std.index(row,0).data().toString(); qDebug() << "mod msecs" << t.msecsTo(QTime::currentTime()); t = QTime::currentTime(); for(int i =0; i< n; ++i) vec[i]; qDebug() << "vec msecs" << t.msecsTo(QTime::currentTime());
Results: 138 msecs (model) vs 18 msecs (vector)
Consider another methodology, let's sort both: model by QSortFilterProxyModel and vector by qSort():
t = QTime::currentTime(); QSortFilterProxyModel srt; srt.setSourceModel(&std); srt.sort(0); qDebug() << "mod sort" << t.msecsTo(QTime::currentTime()); t = QTime::currentTime(); qSort(vec); qDebug() << "vec sort" << t.msecsTo(QTime::currentTime());
Results: 7317 msecs (model) vs 1057 msecs (vector)
Assuming that QSortFilterProxyModel is implemented as much efficient as possible we see that result ~7:1 correlates with previous compares.
So accessing (I emphasize that only accessing) with Qt models may be about 7 times slower than you can reach with stl/qtl.
To my mind to serve Gui it is not critical at all, but if sorting is part of that, then we really may loose performance 7 times. (or any other model manipulations that involve iterations through all items and we have tons of them)
Conclusion and some ideas to squeeze maximum performance from models: for example my implemented models may have some UserRole for accessing const QVector & (of known data type), and then if such model need sorting, I may implement my custom sort model which checks such UserRole for const access to vector and get a sorted through referencing pointers? Will I get performance of sort 7 times? Interesting...
UPDATE: as people mentioned in comments, the difference is also related to inefficient implementation of QStandardItemModel which add overhead on top, so changing to use QStringListModel in "debug" we have 5032 msecs (model) vs 1023 msecs (vector), so performance loose is really less we had in previous testing.
I think it to be continued...