关于qt:QT-日期字符串或时间戳转QDateTime性能测试

52次阅读

共计 3075 个字符,预计需要花费 8 分钟才能阅读完成。

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 <QCoreApplication>
#include <QDateTime>
#include <QSharedPointer>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

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<qint64> 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<QString, void (*)()> funcMap;
    QMap<QString, QList<qint64>> consumeTimeMap;
    QVector<QString> 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<qint64> 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();}

正文完
 0