gpu::morphologyEx在CPU中比morphology Ex慢
gpu::morphologyEx is slower than morphologyEx in CPU?
我正在编写一个c++代码,用于比较opencv的morphologyEx方法使用CPU和GPU版本的性能。这是我的代码:
#include <opencv2/opencv.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <sys/time.h>
#include <ctime>
using namespace cv;
using namespace std;
double start_timer()
{
double start_time = (double) getTickCount();
return start_time;
}
double end_timer(double start_time,int num_tests)
{
double time = (1000 * ((double) getTickCount() - start_time)/ getTickFrequency());
cout << "Average time of " << num_tests << " frames is: " << time/num_tests << " ms" << endl;
return time;
}
int main()
{
Mat cpuSrc;
cv::gpu::GpuMat src_gpu, dst_gpu;
Mat dst;
Mat element;
int element_shape = MORPH_RECT;
element = getStructuringElement(element_shape, Size(10, 10 ), Point(-1, -1) );
cpuSrc = imread("images.jpeg",CV_LOAD_IMAGE_ANYDEPTH);
if (!cpuSrc.data)
{
cerr << "Cannot read the data" << endl;
return -1;
}
cout << "Starting calculating time for CPU ....." << endl;
double start_time = start_timer();
int d = 0;
while(d<100)
{
cv::morphologyEx(cpuSrc, dst, CV_MOP_OPEN, element,Point(-1,-1),1);
}
double total_time_cpu = end_timer(start_time,d);
//--------------------------------------------------------------
cout << "Starting calculating time for GPU ....." << endl;
d = 0;
cv::gpu::GpuMat buf1, buf2;
gpu::Stream stream;
double start_time_1 = start_timer();
while(d<100)
{
stream.enqueueUpload(cpuSrc, src_gpu);
cv::gpu::morphologyEx(src_gpu,dst_gpu,CV_MOP_OPEN,element,
buf1,buf2,Point(-1,-1),1,stream);
stream.enqueueDownload(dst_gpu, dst);
}
stream.waitForCompletion();
double total_time_gpu = end_timer(start_time_1,d);
cout << "Gain is: " << total_time_cpu / total_time_gpu << endl;
return 0;
}
我正在使用一个循环,就好像我在模拟一个包含100帧的视频一样。我使用的是NVIDIA Corporation GF110[GeForce GTX 570]和Intel Corporation Xeon E5/Core i7 DMI2。此外,我测试了上传和下载的时间,第一帧的时间非常大,但之后可以忽略,上传的时间大约为每帧0.02ms,下载的时间为0.1ms,主要消耗的时间是morphologyEx操作。
此模拟的时间结果如下:
对于CPU形态版本100帧的平均时间为:0.027349毫秒,对于GPU版本为:18.0128毫秒
你能帮我弄清楚为什么会有这样出乎意料的表现吗?!!
非常感谢您。
在初始化中,您应该调用:
cv::gpu::setDevice(0);
它将加快初始化速度。
相关文章:
- 无法在"catch(const std::exception &ex)"中捕获 std::invalid_argument
- VirtualAlloc/Ex 分配什么样的内存
- 英特尔 TBB - "初始化关键部分 Ex":找不到标识符编译器错误
- Stroustrup Ex.7, Chap.4 - C++ Syntax
- 复制文件时如何调用取消?(使用复制文件 ex 函数和复制进度例程回调函数)
- 我已经将红皮书 9ed ex. 3.7 从旧函数转换为 4.5 函数,但它没有显示任何内容
- 将多个cmd.exe参数/参数传递给Shellexecute(ex)
- ex.nested try-catch中的什么()更改
- 谁能告诉我如何用字符串初始化 ex 对象
- FMOD EX内存分配问题
- gpu::morphologyEx在CPU中比morphology Ex慢
- 如何为在 .props 文件中声明的非本机 VC 编译器 (GCC) 实现 MSBUILD 文件跟踪功能 (跟踪器.ex
- fstream指向一个特定的路径(到桌面ex.)
- 是否没有GetFilePointer(Ex)Windows API函数
- 无法从 gcc 的 -fdump-tree-gimple 中找出 /[ex] 运算符
- _beginthread/ex C3861 -标识符未找到
- ShellExecute(Ex) with 'properties' 動詞
- Microsoft的复制文件Ex错误
- (Qt或其他)拖放数据的mime类型的uri-list与协议http或ftp在Windows桌面或explorer.ex
- 如何在不使用CreateWindow(Ex)的情况下创建一个窗口(HWND)