有几种工具(除其他外)可以为您进行分析:
其中一些需要特定的编译方式或特定的编译器。其中一些特别擅长针对给定的处理器架构(AMD / Intel ...)进行分析。
由于您似乎可以访问 C++11,并且如果您只想测量一些时间,您可以使用std::chrono
.
#include <chrono>
#include <iostream>
class high_resolution_timer
{
private:
typedef std::chrono::high_resolution_clock clock;
clock::time_point m_time_point;
public:
high_resolution_timer (void)
: m_time_point(clock::now()) { }
void restart (void)
{
m_time_point = clock::now();
}
template<class Duration>
Duration stopover (void)
{
return std::chrono::duration_cast<Duration>
(clock::now()-m_time_point);
}
};
int main (void)
{
using std::chrono::microseconds;
high_resolution_timer timer;
// do stuff here
microseconds first_result = timer.stopover<microseconds>();
timer.restart();
// do other stuff here
microseconds second_result = timer.stopover<microseconds>();
std::cout << "First took " << first_result.count() << " x 10^-6;";
std::cout << " second took " << second_result.count() << " x 10^-6.";
std::cout << std::endl;
}
但是您应该知道,优化几毫秒的整体运行时间几乎没有意义(如果您的程序运行时间将> = 1s)。相反,您应该在代码中计时高度重复的事件(如果有的话,或者至少是那些成为瓶颈的事件)。如果这些显着改善(这可以是毫秒或微秒),您的整体性能也可能会提高。