我使用opencv 3.1,在windows 10和visual studio 2015 update 3中启用了英特尔TBB。第一次转置时间需要100毫秒,另一次转置时间只需要0.02-0.05毫秒。任何人都知道为什么第一次转置需要如此多的时间来处理1*1矩阵。
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发布于 2016-07-28 01:21:06
我没有启用TBB,但问题似乎出在您测量性能的方式上:
1x1矩阵无论如何都没有意义。你可以尝试这样做,然后请让我知道你的执行时间:
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;
}发布于 2016-07-28 10:43:33
感谢您的评论。我试过你的代码,矩阵的创建时间是固定的,第一次转置时间也比其他转置时间多。以下是测试结果。我修改了代码以打印矩阵创建时间和转换时间。
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;
}输出列表如下:
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 https://stackoverflow.com/questions/38618798
复制相似问题