Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why is threading slowing down over time?

If I run the following sample code the calls getting slower and slower:

#include "opencv2\opencv.hpp"
#include <vector>
#include <chrono>
#include <thread>

using namespace cv;
using namespace std;
using namespace std::chrono;

void blurSlowdown(void*) {
    Mat m1(360, 640, CV_8UC3);
    Mat m2(360, 640, CV_8UC3);
    medianBlur(m1, m2, 3);
}

int main()
{
    for (;;) {
        high_resolution_clock::time_point start = high_resolution_clock::now();

        for (int k = 0; k < 100; k++) {
            thread t(blurSlowdown, nullptr);
            t.join();
        }

        high_resolution_clock::time_point end = high_resolution_clock::now();
        cout << duration_cast<microseconds>(end - start).count() << endl;
    }
}

Why does that happen? Is it a bug?

Some observations / remarks:
-The effect is amplified when using the debug Library, even if no debugger is used
-The processor boost clock is stable and is not causing the problem
-It does not matter if m1 and m2 are allocated on the heap instead of the stack
-The slowdown effect does not appear when medianBlur isn't called

My system:
-Windows 10 64bit
-MSVC compiler
-Newest offical OpenCV 3.4.2 binarys

Why is threading slowing down over time?

If I run the following sample code the calls getting slower and slower:

#include "opencv2\opencv.hpp"
#include <vector>
#include <chrono>
#include <thread>

using namespace cv;
using namespace std;
using namespace std::chrono;

void blurSlowdown(void*) {
    Mat m1(360, 640, CV_8UC3);
    Mat m2(360, 640, CV_8UC3);
    medianBlur(m1, m2, 3);
}

int main()
{
    for (;;) {
        high_resolution_clock::time_point start = high_resolution_clock::now();

        for (int k = 0; k < 100; k++) {
            thread t(blurSlowdown, nullptr);
            t.join();
        }

        high_resolution_clock::time_point end = high_resolution_clock::now();
        cout << duration_cast<microseconds>(end - start).count() << endl;
    }
}

Why does that happen? Is it a bug?

Some observations / remarks:
-The effect is amplified when using the debug Library, even if no debugger is used
-The processor boost clock is stable and is not causing the problem
-It does not matter if m1 and m2 are allocated on the heap instead of the stack
-The slowdown effect does not appear when medianBlur isn't called

My system:
-Windows 10 64bit
-MSVC compiler
-Newest offical OpenCV 3.4.2 binarysbinaries