上司突然说depth_camera的cpu太高了,其实加入amp filter以后经过调整也没有比以前高,但是人家说高,那就想办法降低呗,毕竟打工人,不过经过这个过程也学到了一些东西。
1.C++程序性能分析
想降低cpu消耗首先要知道那里消耗的cpu多,好对症下药。这里使用了google的性能分析工具google-perftools.
1)先到github上下载源代码:https://github.com/gperftools/gperftools/releases
(当时用的是2.7版本https://github.com/gperftools/gperftools/releases/download/gperftools-2.7/gperftools-2.7.tar.gz)
按照说明进行编译,因为我们是rk3399,所以去百度一下怎么在arm上编译
tar -xvf gperftools-2.7.tar.gz //解压并进入其源码根目录
./autogen.sh
./configure
make && make install // 相关库和头文件将被安装到 /usr/local 目录下
编译好了以后把生成的库文件、头文件都搞到自己的工程里.具体是下边这些文件:
/usr/local/include/gperftools/
/usr/local/lib/lib{profiler, tcmalloc*}.*
/usr/local/lib/pkgconfig/lib{profiler, tcmalloc*}.*
2)在自己的工程里使用google-perftools,有两种方法
第一种
#include<google/profiler.h>
void myfunc()
{
ProfilerStart("my.prof");
// things to do
ProfilerStop();
}
第二种
#include <google/profiler.h>
#include <signal.h>
using namespace std;
static void gprof_callback(int signum)
{
if(signum == SIGUSR1)
{
cout << "Catch signal ProfilerStart\n" << endl;
ProfilerStart("my.prof");
}
else if(signum == SIGUSR2)
{
cout << "Catch signal ProfilerStop\n" << endl;
ProfilerStop();
}
}
static void setup_signal()
{
struct sigaction profstat;
profstat.sa_handler = gprof_callback;
profstat.sa_flags = 0;
sigemptyset(&profstat.sa_mask);
sigaddset(&profstat.sa_mask, SIGUSR1);
sigaddset(&profstat.sa_mask, SIGUSR2);
if(sigaction(SIGUSR1, &profstat, NULL) < 0)
cout << "SIGUSR1! Fail !" << endl;
if(sigaction(SIGUSR2, &profstat, NULL) < 0)
cout << "SIGUSR2! Fail !" << endl;
}
void myfunc()
{
setup_signal();
// things to do
}
在嵌入式设备上运行
kill -s SIGUSR1 $pid //分析开始
kill -s SIGUSR2 $pid //结束分析
3).prof 文件转换
sudo apt-get install kcachegrind
pprof --callgrind ./myprogram xxx.prof > xxx.callgrind
kcachegrind xxx.callgrind
参考:介绍一个对陌生程序快速进行性能瓶颈分析的技巧 - 皇家救星 - 博客园
2.程序的优化
经过分析发现是一些opencv的操作比较耗时,一些经验
1)降低分辨率对速度提升很明显
2)cv::Mat的遍历方式也很重要 【OpenCV】访问Mat中每个像素的值(新)_小魏的修行路-CSDN博客_opencv访问像素
3)有的时候自己写遍历可能更快一些