陳文盛
陳文盛

Reputation: 31

opencv transpose first time need long time

I use opencv 3.1 and enable intel TBB in windows 10 with visual studio 2015 update 3. The first transpose time need 100 ms and the other transpose just need 0.02-0.05 ms. Any know why the first transpose need so much time for 1*1 matrix.

double ts = time_measure("start", 0);
Mat_<uchar> A = (Mat_<uchar>(1, 1) << 1);
Mat at = A.t();
cout << "transpose Times needed : " << time_measure("end", ts) * 1000 << " ms " << endl;

for (int i = 0; i < 10; i++) {
    ts = time_measure("start", 0);
    Mat_<uchar> B = (Mat_<uchar>(1, 1) << 1);
    Mat bt = B.t();
    cout << "transpose Times needed : " << time_measure("end", ts) * 1000 << " ms " << endl;
}


double time_measure(const string mode, double ts) {
    double t = 0.0;
    if (mode == "start") {
       t = (double)getTickCount();
    }
    else {
       t = ((double)getTickCount() - ts) / getTickFrequency();
    }
    return t;
}

The output

transpose A Times needed : 112.062 mstranspose B Times needed : 0.0337221 ms
transpose B Times needed : 0.0205265 ms
transpose B Times needed : 0.0195491 ms
transpose B Times needed : 0.0283461 ms
transpose B Times needed : 0.0234589 ms
transpose B Times needed : 0.0298123 ms
transpose B Times needed : 0.0249251 ms
transpose B Times needed : 0.0283461 ms
transpose B Times needed : 0.0273687 ms
transpose B Times needed : 0.02688 ms

Upvotes: 1

Views: 263

Answers (2)

陳文盛
陳文盛

Reputation: 31

Thanks for your comment. I had try your code and the matrix creation time is fixed and the first transpose time also take the much time than the other transpose. the following is the test result. I modify the code to print the matrix creation time and tranpose time.

int _tmain(int argc, _TCHAR* argv[]) {
    for (int i = 0; i < 10; i++) {
        double ts = time_measure(true, 0);
        // 1000 x 1000 random matrix
        Mat_<uchar> B(1000, 1000);
        randu(B, 0, 256);
        cout << "create matrix Times needed : " << time_measure(false, ts) * 1000 << " ms " << endl;

        ts = time_measure(true, 0);
        Mat bt = B.t();
        cout << "transpose Times needed : " << time_measure(false, ts) * 1000 << " ms " << endl;
    }
}

double time_measure(bool start, double ts) {
double t = 0.0;
if (start) {
    t = (double)getTickCount();
}
else {
    t = ((double)getTickCount() - ts) / getTickFrequency();
}
return t;
}

the ouput is list following:

create matrix Times needed : 49.3267 ms
transpose Times needed : 427.299 ms
create matrix Times needed : 51.8431 ms
transpose Times needed : 0.889971 ms
create matrix Times needed : 51.8084 ms
transpose Times needed : 0.718917 ms
create matrix Times needed : 52.4946 ms
transpose Times needed : 0.742376 ms
create matrix Times needed : 45.5454 ms
transpose Times needed : 0.705721 ms
create matrix Times needed : 45.218 ms
transpose Times needed : 0.70621 ms
create matrix Times needed : 44.5748 ms
transpose Times needed : 0.713541 ms
create matrix Times needed : 46.2501 ms
transpose Times needed : 0.68715 ms
create matrix Times needed : 45.153 ms
transpose Times needed : 0.663691 ms
create matrix Times needed : 44.1892 ms
transpose Times needed : 0.584028 ms 

Upvotes: 0

Miki
Miki

Reputation: 41765

I don't have TBB enabled, but the issue seems with the way you're measuring the performance:

  1. don't include the time to create the matrix
  2. use a matrix large enough. It doesn't make sense to transpose a 1x1 matrix anyway.
  3. don't use strings for booleans

You could try something like this, and then please let me know your execution times:

double time_measure(bool start, double ts) {
    double t = 0.0;
    if (start) {
        t = (double)getTickCount();
    }
    else {
        t = ((double)getTickCount() - ts) / getTickFrequency();
    }
    return t;
}

int main()
{
    for (int i = 0; i < 10; i++) {

        // 1000 x 1000 random matrix
        Mat_<uchar> B(1000, 1000);
        randu(B, 0, 256);

        double ts = time_measure(true, 0);
        Mat bt = B.t();
        cout << "transpose Times needed : " << time_measure(false, ts) * 1000 << " ms " << endl;
    }

    getchar();
    return 0;
}

Upvotes: 1

Related Questions