CPP 中速度性能的测试功能
Testing Function for speed performance in CPP
>我创建了一个简单的函数将任何小写字母 a-z 转换为大写,问题可能不是问题,但每个测试都返回 0。如果我添加系统("暂停"),我可以看到一个新值,指示暂停的长度。
有没有更准确的方法来测试速度,或者这实际上是正确的?我想将其与其他函数进行比较,看看它的转换速度是否比标准函数快。
char* ToUppercase(char* Input)
{
int Len = Length(Input);
for (int i = 0; i < Len; i++)
{
short keycode = static_cast<short>(Input[i]);
if (keycode >= 97 && keycode <= 122)
Input[i] -= 32;
}
return Input;
}
我用来测试的当前计时器是(由其他人创建)
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
template<typename F, typename ...Args>
static typename TimeT::rep execution(F func, Args&&... args)
{
auto start = std::chrono::system_clock::now();
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast< TimeT>
(std::chrono::system_clock::now() - start);
return duration.count();
}
};
要调用我使用:
void Debug()
{
char Buffer[10000] = "aaaa /..../ aaaa";
MyStringControl::ToUppercase(Buffer);
}
int main()
{
std::cout << measure<std::chrono::nanoseconds>::execution(Debug);
}
你看过std::chrono::high_resolution_clock
吗?
下面是一个示例:
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
template<typename F, typename ...Args>
static typename TimeT::rep execution(F func, Args&&... args)
{
auto start = std::chrono::high_resolution_clock::now();
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast< TimeT>
(std::chrono::high_resolution_clock::now() - start);
return duration.count();
}
};
int total = 0;
void test()
{
int foo = 0;
for (int i=0; i<1000; ++i) ++foo;
total += foo;
}
int main ()
{
using namespace std::chrono;
for (int i = 0; i < 30; ++i)
{
total = 0;
auto t = measure<std::chrono::nanoseconds>::execution(test);
std::cout << "Calculated total = " << total << " in " << t << " ns." << std::endl;
}
return 0;
}
这给了:
Calculated total = 1000 in 64 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 22 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 13 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 13 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 13 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 22 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 14 ns.
运行函数 1000000 次,然后将结果除以 1000000。 您可以使用高精度计时器,但由于硬件的怪癖,它更容易出现不准确。
编辑:
您希望对函数本身进行 1000,000 次调用,并且只调用一次计时器:
auto start = std::chrono::system_clock::now();
for (size_t counter = 0; counter<1000000; ++counter)
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast< TimeT>
(std::chrono::system_clock::now() - start)/1000000;
return duration.count();
你的函数Debug
什么都不做,你的编译器可能能够弄清楚这一点,因此你所做的只是计时连续两次调用now
的速度。
做一些事情来确保你试图计时的代码不会被优化掉。 例如,以某种方式使用它的输出,或者给它__attribute__((noinline))
(如果你不介意计时实际函数调用的成本)或其他东西。
(此外,如果您希望从计时中获得任何有用的精度,则需要函数花费比时钟分辨率长得多的时间)
相关文章:
- G++10 的功能测试在 __cpp_lib_span 上失败
- 单位测试私人方法通过使其自由功能
- 单位测试QT中的非返回功能
- 功能测试宏在 Visual C++ 中无法正常工作
- 在Google测试框架中,如何期望函数调用或其他功能调用
- 谷歌测试的重载功能
- (指针)降低测试分数后计算平均功能的问题
- 测试模拟功能的最佳方法
- 支架检查功能仅测试输入的前 2 个
- 相等性测试功能
- 更改单元测试的类功能的正确方法
- 如何使用C++测试功能支持
- 在Linux Mint上编译Qt-OpenGL功能测试失败
- 回文测试功能
- 测试C 轮询插座功能
- CPPUNIT:我们真的每个测试需要一个功能吗?
- Mac的McCabe式功能复杂度测试
- 如何使用Google测试在Main()函数中的特定位置调用特定的测试功能
- CPP 中速度性能的测试功能
- 我可以将参数传递给谷歌测试功能吗