Benchmark测试
最后更新于:2022-04-02 02:09:59
[TOC]
## 概述
### 编写一个基准测试函数
```
void TestBenchmark::simple()
{
QString str1 = QLatin1String("This is a test string");
QString str2 = QLatin1String("This is a test string");
QCOMPARE(str1.localeAwareCompare(str2), 0);
QBENCHMARK {
str1.localeAwareCompare(str2);
}
}
```
### 多数据输入的基准测试
```
void TestBenchmark::multiple_data()
{
QTest::addColumn("useLocaleCompare");
QTest::newRow("locale aware compare") << true;
QTest::newRow("standard compare") << false;
}
void TestBenchmark::multiple()
{
QFETCH(bool, useLocaleCompare);
QString str1 = QLatin1String("This is a test string");
QString str2 = QLatin1String("This is a test string");
int result;
if (useLocaleCompare)
{
QBENCHMARK {
result = str1.localeAwareCompare(str2);
}
}
else
{
QBENCHMARK {
result = (str1 == str2);
}
}
}
```
';