0

我已经编写了一些代码来使用 OpenCV 加载 1025 个图像来处理它们;这些代码有两个版本:单线程和多线程;问题是代码的结果让我感到困惑;因为单线程版本比多线程更快。

你怎么看待这件事??怎么了 ?

我的代码如下。

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include "ctpl.h"

using namespace std;
using namespace cv;

#define threaded

void loadImage(int id, int param0) {
    stringstream stream;
    stream << "/home/me/Desktop/Pics/pic (" << param0 << ").jpg";
    Mat x = imread(stream.str(), IMREAD_REDUCED_COLOR_8);
}

int main() {
#ifdef threaded
    ctpl::thread_pool p(8);
    for (int i = 1; i <= 1025; i++) {
        p.push(loadImage,i);
    }
//    for (int i = 0; i < 1025; ++i) {
//        pthread_join(threads[i], NULL);
//    }
#else
    for (int i = 1; i <= 1025; i++) {
        stringstream stream;
        stream << "/home/me/Desktop/Pics/pic (" << i << ").jpg";
        Mat x = imread(stream.str(), IMREAD_REDUCED_COLOR_8);
    }
#endif
    return 0;
}
4

0 回答 0