我刚刚在新的 OpenCV 2.4.3 中看到他们添加了一个通用的 parallel_for。所以按照这个例子,我尝试自己实现它。我用我的代码让它全部运行,但是当我用常规的“for”命令以典型的串行方式完成它的处理与类似的循环时,结果快得微不足道,或者通常慢一点!
我想这可能与我推入向量或其他东西有关(我是并行处理的一个相当大的菜鸟),所以我设置了一个只运行一个大数字的测试循环,它仍然不起作用。
代码:
class Parallel_Test : public cv::ParallelLoopBody
{
private:
double* const mypointer;
public:
Parallel_Test(double* pointer)
: mypointer(pointer){
}
void operator() (const Range& range) const
{
//This constructor needs to be here otherwise it is considered an abstract class.
// qDebug()<<"This should never be called";
}
void operator ()(const cv::BlockedRange& range) const
{
for (int x = range.begin(); x < range.end(); ++x){
mypointer[x]=x;
}
}
};
//TODO Loop pixels in parallel
double t = (double)getTickCount();
//TEST PARALELL LOOPING AT ALL
double data1[1000000];
cv::parallel_for(BlockedRange(0, 1000000), Parallel_Test(data1));
t = ((double)getTickCount() - t)/getTickFrequency();
qDebug() << "Parallel TEST time " << t << endl;
t = (double)getTickCount();
for(int i =0; i<1000000; i++){
data1[i]=i;
}
t = ((double)getTickCount() - t)/getTickFrequency();
qDebug() << "SERIAL Scan time " << t << endl;
输出:
Parallel TEST time 0.00415479
SERIAL Scan time 0.00204597