我正在跟踪一个方法在其内部和外部的执行时间。我使用ROS来获得时间,因为这一切都在ROS包中,而且非常方便。文档可以在这里找到:http://wiki.ros.org/roscpp/Overview/Time。
void ClassName::my_method()
{
ros::Time t_start = ros::Time::now();
// do stuff
ros::Duration d_execution = ros::Time::now() - t_start;
}
void ClassName::Func()
{
ros::Time t_total = ros::Time::now();
my_method();
ros::Duration d_total = ros::Time::now() - t_total;
}我的问题是d_total总是比d_execution长得多。
d_execution通常为20-40微秒,但d_total大约为150微秒。
我不明白为什么会发生这样的事情。我的理解是,在方法的末尾发生的唯一一件事就是释放在方法中堆栈上创建的任何内存。然而,我已经重写了代码,只创建了大约一半数量的变量,并且我在d_total中看不到任何变化。还会发生什么事情让函数花这么长时间才返回?任何帮助都是非常感谢的。
编辑:我测量了ros:: time ::now()的时间,它似乎可以忽略不计。
time_t now, then;
time(&now);
ros::Time t = ros::Time::now();
time(&then);
printf("t1-t0: %f", difftime(now, then));输出为"t1-t0: 0.000000“。
此外,该方法中在堆栈上分配的所有变量都是双精度的。唯一的例外是两个std::vector,它们只包含2-3个元素。
发布于 2016-05-28 03:26:03
与my_method()确切版本有多大不同?
my_method是虚拟的吗?
在进行性能测试时,更自然的方式是显示百分位数而不是平均值。
这是测量差异的更正确的方法:
void ClassName::my_method()
{
ros::Time t_start = ros::Time::now();
{ // note this
// do stuff
} // and node this, destructors will be run before the next line
ros::Duration d_execution = ros::Time::now() - t_start;
}在Linux/OSX下,您可以尝试使用clock_gettime(),这是我在性能测试中通常使用的。在C++中,您可以尝试std::chrono::duration (http://en.cppreference.com/w/cpp/chrono/duration)。也许ros::Time有一个很大的抖动时间。
发布于 2016-05-28 03:22:38
在返回时,方法调用析构函数来处理在其最高级别中声明的所有内容。这些析构函数可以调用更多的析构函数,以此类推。
此外,在返回时,可以恢复寄存器值,但这几乎不需要100微秒。
释放堆栈内存是通过更改堆栈指针值来完成的,这是非常快的(可能是一个命令)。
https://stackoverflow.com/questions/37490813
复制相似问题