@DerReisende
Thank you very much for your hint! I have declared two private class members:
QHash<QDate, QString> m_dateCache;
QHash<QDateTime, QString> m_dateTimeCache;
In my implementation I alternate between method calls that use cache lookup and those just doing the conversion.
bool LogTableModel::saveDataToFile(QString fileName, bool useCache)
My actual use of the cache is here:
if (useCache) {
QDate d = this->m_columns.at(m_dateAssignedColumn)->at(i).toDate();
QString sDate = m_dateCache.value(d,QString());
if (sDate.isEmpty()) {
sDate = d.toString(this->dateFormat);
m_dateCache.insert(d,sDate);
}
itemObj["dateAssigned"] = sDate;
} else {
itemObj["dateAssigned"] = this->m_columns.at(m_dateAssignedColumn)->at(i).toDate().toString(this->dateFormat);
}
The results show, that with cacheAlternate == true the code is a tad quicker indeed, but only by some 3% (not counting the slower initial run). Using the same approach on the DateTime field actually makes the code slower, for whatever reason. Probably because I never have two identical DateTimes, so the QHash becomes rather big.
I have tried to cache the QVariant instead, to get rid of the "toDate()":
QHash<QVariant, QString> m_dateCache;
but my lookup
QString sDate = m_dateCache.value(v,QString());
makes the compiler claim about "no matching function call for qHash", QVariant does not seem to have a proper qHash() function for this - and I wouldn't expect any magical acceleration here either.
But even if it's not a huge boost, I've learned much from this hint. Thank you very much for the input!
I'll try the mutex idea next.