我创建了一个简单的函数将任何小写字母az转换为大写,问题可能不是问题,但每个测试都返回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);
}
答案 0 :(得分:3)
你看过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.
答案 1 :(得分:2)
运行功能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();
答案 2 :(得分:1)
你的函数Debug
什么也没做,你的编译器也许可以解决这个问题,因此你所做的就是计算连续两次调用now
的速度。< / p>
做一些事情以确保您尝试计时的代码不会被优化掉。例如以某种方式使用它的输出,或者给它__attribute__((noinline))
(如果你不介意计算实际函数调用的成本)或者什么。
(另外,如果你想要从时间上获得任何有用的精确度,你需要你的功能大大超过时钟的分辨率)