【发布时间】:2015-06-25 12:46:43
【问题描述】:
我创建了一个简单的函数来将任何小写字母 az 转换为大写,这个问题可能不是问题,但每个测试都返回 0。如果我添加 system("pause") 我可以看到一个新值,指示长度暂停。
是否有更准确的方法来测试速度,或者这实际上是否正确?我想将它与其他函数进行比较,看看它是否比标准函数转换得更快。
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了吗? -
我有,但它仍然返回 0。我开始怀疑速度是否太快(四核 i7 cpu),所以我使用了这个:start high_res_clock for(int i = 0; i
-
我下面的代码是否在您的系统上给出了 0?我也在使用快速核心 i7 (3.5 GHz) - 仍然不应该是 0。:) (我只是编辑了发布时间而不是调试,我的仍然非零。)
-
我注意到评论“
修复:时钟被重写以保持一致和精确。”这里:blogs.msdn.com/b/vcblog/archive/2014/06/03/… -
当它给你暂停的长度时,它是否以纳秒为单位给你?我还在想一辆越野车
duration_cast。
标签: c++ c++11 performance-testing