QT里将字符串或时间戳转换为QDateTime不同方法性能比较(release编译下)
环境:
Qt:6.2.3(MSVC 2019, 64 bit)
QtCreator:7.0.2
c++:17
boost:1.8
方法:
1.使用QDateTime自带的fromString方法
不考虑如下函数,因为Qt::TextDate解析方式限制死了字符串格式,必须为"ddd MMM d HH:mm:ss yyyy"
QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format = Qt::TextDate)
使用如下函数
QDateTime QDateTime::fromString(QStringView string, QStringView format, QCalendar cal = QCalendar())
2.使用substring按字符串格式进行字符串截取,然后调用QDateTime构造方法
3.将字符串传给boost库的posix_time的fromstring,然后调用QDateTime构造方法
4.使用时间戳方式
结论
substring->时间戳->QDateTime::fromString->Boost_posix_time;
10000条
100000条
测试代码
#include
#include
#include
#include
#include
using namespace boost::posix_time;
const static std::string dateTimeStdStr = "2022/10/12 08:10:13";
const static QString dateTimeFormat = "yyyy/MM/dd HH:mm:ss";
const static qint64 dateTimeStamp = 1669362601364;
QString printResult(QList list) {
qint64 total = 0;
QString result = "";
for (int i = 0; i < list.size(); i++) {
result += QString("%1ms,").arg(list.at(i));
total += list.at(i);
}
result += QString("平均:%1ms").arg(total / list.size());
return result;
}
int main(int argc, char* argv[]) {
QCoreApplication a(argc, argv);
// init data
int size = 100000, testTimes = 10;
QMap funcMap;
QMap> consumeTimeMap;
QVector vector = {"String:调用QDateTime::fromString方法转换", "String:substring截取交给QDateTime构造",
"String:传给boost的fromstring,再交给QDateTime构造",
"Int时间戳:调用QDateTime::fromMSecsSinceEpoch方法转换"};
funcMap.insert(vector.at(0),
[]() { QDateTime temp = QDateTime::fromString(dateTimeStdStr.c_str(), dateTimeFormat); });
funcMap.insert(vector.at(1), []() {
int year = atoi(dateTimeStdStr.substr(0, 4).c_str());
int month = atoi(dateTimeStdStr.substr(5, 2).c_str());
int day = atoi(dateTimeStdStr.substr(8, 2).c_str());
int hour = atoi(dateTimeStdStr.substr(11, 2).c_str());
int minute = atoi(dateTimeStdStr.substr(14, 2).c_str());
int second = atoi(dateTimeStdStr.substr(17, 2).c_str());
QDateTime temp = QDateTime(QDate(year, month, day), QTime(hour, minute, second));
});
funcMap.insert(vector.at(2), []() {
const std::locale loc = std::locale(std::locale::classic(), new time_input_facet("%Y-%m-%d %H:%M:%S"));
std::istringstream is(dateTimeStdStr);
is.imbue(loc);
boost::posix_time::ptime t;
is >> t;
tm pt_tm = to_tm(t);
QDateTime dateTime = QDateTime(QDate(pt_tm.tm_year, pt_tm.tm_mon, pt_tm.tm_mday),
QTime(pt_tm.tm_hour, pt_tm.tm_min, pt_tm.tm_sec));
});
funcMap.insert(vector.at(3), []() { QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(dateTimeStamp); });
// start test
qDebug() << QString("开始测试,数据量为%1,测试次数为%2").arg(size).arg(testTimes);
for (int i = 0; i < vector.size(); i++) {
QString key = vector.at(i);
QList consumeTimeList;
for (int j = 0; j < testTimes; j++) {
qint64 startTime = QDateTime::currentMSecsSinceEpoch();
void (*func)() = funcMap.value(key);
for (int k = 0; k < size; k++) {
func();
}
qint64 endTime = QDateTime::currentMSecsSinceEpoch();
consumeTimeList.push_back(endTime - startTime);
}
consumeTimeMap.insert(key, consumeTimeList);
}
for (int i = 0; i < vector.size(); i++) {
QString key = vector.at(i);
qDebug() << QString("测试%1结束,执行情况(%2)").arg(key).arg(printResult(consumeTimeMap.value(key)));
}
return a.exec();
}