当前位置:   article > 正文

【学习体会】OpenCV的cv::parallel_for_使用_cv::paralare

cv::paralare

仅仅只是多线程的话,cv::parallel_for_并不会比openmp快多少,不相上下

  1. #include<opencv2/opencv.hpp>
  2. #include<omp.h>
  3. #include<thread>
  4. #include <stdio.h>
  5. #include <chrono>
  6. #include<iostream>
  7. class LoopBody : public cv::ParallelLoopBody
  8. {
  9. public:
  10. LoopBody (int*_a,int*_b,int*_c):a(_a),b(_b),c(_c)
  11. {
  12. }
  13. virtual void operator()(const cv::Range& r) const
  14. {
  15. for (int i = r.start; i != r.end; i++) //遍历
  16. {
  17. c[i] = a[i]+b[i];
  18. }
  19. }
  20. protected:
  21. int *a;
  22. int *b;
  23. int *c;
  24. };
  25. int main(int argc, char** argv)
  26. {
  27. int num = 10000;
  28. {
  29. int* a = new int[num];
  30. int* b = new int[num];
  31. int* c = new int[num];
  32. for(int i=0;i<num;i++){
  33. a[i]=1;
  34. b[i]=2;
  35. c[i]=0;
  36. }
  37. if(num<=10){
  38. for(int i=0;i<num;i++){
  39. std::cout<<c[i]<<" ";
  40. }
  41. std::cout<<std::endl<<std::endl;
  42. }
  43. std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
  44. #pragma omp parallel for num_threads(2 * omp_get_num_procs() - 1)
  45. for(int i=0;i<num;i++){
  46. //printf("thread ID %d\n", std::this_thread::get_id());
  47. c[i] = a[i]+b[i];
  48. }
  49. std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
  50. std::chrono::microseconds duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  51. std::cout << "openmp Spent " << double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den << " seconds." << std::endl;
  52. if(num<=10){
  53. for(int i=0;i<num;i++){
  54. std::cout<<c[i]<<" ";
  55. }
  56. std::cout<<std::endl<<std::endl;
  57. }
  58. delete[] a;
  59. delete[] b;
  60. delete[] c;
  61. }
  62. {
  63. int* a = new int[num];
  64. int* b = new int[num];
  65. int* c = new int[num];
  66. for(int i=0;i<num;i++){
  67. a[i]=1;
  68. b[i]=2;
  69. c[i]=0;
  70. }
  71. if(num<=10){
  72. for(int i=0;i<num;i++){
  73. std::cout<<c[i]<<" ";
  74. }
  75. std::cout<<std::endl<<std::endl;
  76. }
  77. std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
  78. cv::setNumThreads(2 * omp_get_num_procs() - 1);
  79. LoopBody body(a,b,c);
  80. cv::parallel_for_(cv::Range(0, num), body);
  81. std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
  82. std::chrono::microseconds duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  83. std::cout << "parallel_for Spent " << double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den << " seconds." << std::endl;
  84. if(num<=10){
  85. for(int i=0;i<num;i++){
  86. std::cout<<c[i]<<" ";
  87. }
  88. std::cout<<std::endl<<std::endl;
  89. }
  90. delete[] a;
  91. delete[] b;
  92. delete[] c;
  93. }
  94. return 0;
  95. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/657421
推荐阅读
相关标签
  

闽ICP备14008679号