赞
踩
仅仅只是多线程的话,cv::parallel_for_并不会比openmp快多少,不相上下
- #include<opencv2/opencv.hpp>
- #include<omp.h>
- #include<thread>
- #include <stdio.h>
- #include <chrono>
- #include<iostream>
- class LoopBody : public cv::ParallelLoopBody
- {
- public:
- LoopBody (int*_a,int*_b,int*_c):a(_a),b(_b),c(_c)
- {
- }
- virtual void operator()(const cv::Range& r) const
- {
- for (int i = r.start; i != r.end; i++) //遍历
- {
- c[i] = a[i]+b[i];
- }
- }
-
- protected:
- int *a;
- int *b;
- int *c;
- };
-
- int main(int argc, char** argv)
- {
- int num = 10000;
-
- {
- int* a = new int[num];
- int* b = new int[num];
- int* c = new int[num];
-
- for(int i=0;i<num;i++){
- a[i]=1;
- b[i]=2;
- c[i]=0;
- }
- if(num<=10){
- for(int i=0;i<num;i++){
- std::cout<<c[i]<<" ";
- }
- std::cout<<std::endl<<std::endl;
- }
-
- std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
-
- #pragma omp parallel for num_threads(2 * omp_get_num_procs() - 1)
- for(int i=0;i<num;i++){
- //printf("thread ID %d\n", std::this_thread::get_id());
- c[i] = a[i]+b[i];
- }
-
- std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
- std::chrono::microseconds duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
- std::cout << "openmp Spent " << double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den << " seconds." << std::endl;
-
- if(num<=10){
- for(int i=0;i<num;i++){
- std::cout<<c[i]<<" ";
- }
- std::cout<<std::endl<<std::endl;
- }
- delete[] a;
- delete[] b;
- delete[] c;
- }
-
- {
- int* a = new int[num];
- int* b = new int[num];
- int* c = new int[num];
-
- for(int i=0;i<num;i++){
- a[i]=1;
- b[i]=2;
- c[i]=0;
- }
- if(num<=10){
- for(int i=0;i<num;i++){
- std::cout<<c[i]<<" ";
- }
- std::cout<<std::endl<<std::endl;
- }
- std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
- cv::setNumThreads(2 * omp_get_num_procs() - 1);
- LoopBody body(a,b,c);
- cv::parallel_for_(cv::Range(0, num), body);
-
-
- std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
- std::chrono::microseconds duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
- std::cout << "parallel_for Spent " << double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den << " seconds." << std::endl;
- if(num<=10){
- for(int i=0;i<num;i++){
- std::cout<<c[i]<<" ";
- }
- std::cout<<std::endl<<std::endl;
- }
- delete[] a;
- delete[] b;
- delete[] c;
- }
-
-
-
- return 0;
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。