无论我尝试什么,我都无法得到一个简单的计时器。如何查看一段代码在没有外部库的情况下运行需要多少毫秒?定时功能
我曾尝试:
time_t total = static_cast<time_t>(0.0f);
for(int i = 0; i < 10; ++i)
{
time_t start = time(0);
for(int b = 0; b < 100; ++b)
{
newMesh.IsValid();
}
time_t end = time(0);
total += (end - start);
}
time_t average = total/10;
printf("Average time of Knight IsValid check %d\n", average);
这大约需要15秒,说花了1毫秒。我也尝试过:
std::clock_t total = static_cast<time_t>(0.0f);
for(int i = 0; i < 10; ++i)
{
std::clock_t start = std::clock();
for(int b = 0; b < 100; ++b)
{
newMesh.IsValid();
}
std::clock_t end = std::clock();
total += (end - start);
}
std::clock_t average = total/10;
printf("Average time of Knight IsValid check %d\n", average);
但我被告知那是时钟滴答,不适合剖析?
上有[cppreference.com](http://en.cppreference.com/w/cpp/chrono/steady_clock/now),我一个非常简单的例子认为会做你需要的。 – 5gon12eder
似乎没有chrono?我正在使用Visual Studio 2012. – marsh
这是一个C++ 11功能。也许你需要为GCC添加一个特殊的编译器标志,比如'-std = C++ 11'或者升级你的编译器。这将是值得的事情。 – 5gon12eder