I am using the library of boost to create a 2D dimensions array
and compare about their speed
There are three kinds of array, matrix of boost, multi_array of boost and the raw array
#ifndef TESTRELMAT_H
#define TESTRELMAT_H
#define BOOST_NO_EXCEPTIONS
#define BOOST_DISABLE_ASSERTS
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/multi_array.hpp>
using boost::multi_array;
using boost::numeric::ublas::matrix;
#define _SECURE_SCL 0
#include<vector>
#include<algorithm>
#include<ctime>
void testRelMat()
{
constint row = 1000;
constint column = 1000;
std::vector<unsignedchar> sorted( 9 );
std::clock_t begin, end;
matrix<unsignedchar> data(row, column);
typedef boost::numeric::ublas::zero_matrix<unsignedchar> zMat;
matrix<unsignedchar> temp( data.size1() + 2, data.size2() + 2);
int k = 0;
begin = std::clock();
for(unsignedint i = 0; i < data.size1(); i++)
for(unsignedint j = 0; j < data.size2(); j++)
{
for( unsignedint l = 0; l< 3; l++)
for( unsignedint m = 0; m < 3; m++)
{
sorted[k] = temp(i + l, j + m);
k++;
}
k = 0;
std::sort(sorted.begin(), sorted.end());
data.insert_element( i, j, sorted[4] );
}
end = std::clock();
cout<<"time of ublas : "<<(double)(end-begin)/CLOCKS_PER_SEC<<" "<<endl;
multi_array<unsignedchar, 2> dataArr(boost::extents[row][column]);
multi_array<unsignedchar, 2> tempArr(boost::extents[row + 2][column + 2]);
int medium = 0;
begin = std::clock();
for(unsignedint i = 0; i < data.size1(); i++)
for(unsignedint j = 0; j < data.size2(); j++)
{
for( unsignedint l = 0; l< 3; l++)
for( unsignedint m = 0; m < 3; m++)
{
sorted[medium] = tempArr[i + l][j + m];
medium++;
}
medium = 0;
std::sort(sorted.begin(), sorted.end());
dataArr[i][j] = sorted[4];
}
end = std::clock();
cout<<"time of multi array : "<<(double)(end-begin)/CLOCKS_PER_SEC<<" "<<endl;
unsignedchar **pp;
pp = newunsignedchar* [column];
for(unsignedint i = 0; i < column; i++)
*(pp + i)=newunsignedchar[row];
unsignedchar **temp2 = newunsignedchar *[row + 2];
for(unsignedint i = 0; i < row + 2; ++i)
*(temp2 + i) = newunsignedchar[column + 2];
int med = 0;
begin = std::clock();
for(unsignedint i = 0; i < column; i++)
for(unsignedint j = 0; j < row; j++)
{
for( unsignedint l = 0; l< 3; l++)
for( unsignedint m = 0; m < 3; m++)
{
sorted[med] = temp2[i + l][j + m];
med++;
}
med = 0;
std::sort(sorted.begin(), sorted.end());
pp[i][j] = sorted[4];
}
end = std::clock();
cout<<"time of raw array : "<<(double)(end-begin)/CLOCKS_PER_SEC<<" "<<endl;
for(unsignedint i = 0; i < column; ++i)
delete [] pp[i];
delete [] pp;
for(unsignedint i = 0; i < 290; ++i)
delete [] temp2[i];
delete [] temp2;
/*unsigned char** pp = unsigned char* [column];
pp[0] = unsigned char [column * row];
for (unsigned int i = 1; i < column; ++i)
pp[i] = pp[i-1] + row;
delete [] pp[0];
delete [] pp;*/
}
#endif
My compiler is visualC++ 2008 express edition, my OS is windows xp sp3. Below is the results of the release version.
1 2 3 4 5 6
time of ublas : 0.156
time of multi_array : 0.172
time of raw array : 0.094
Obviously, the raw array is faster than the dynamic array of ublas and multi_array. Do I made any mistakes? Are there any ways
could improve the speed?Thanks
Could you show me your code?Are they same as mine?
Looks like the speed of multi array and raw array are much more faster than visual c++ 2008
But the speed of ublas are the other story
I prefer ublas because I may need to do some calcution about matrix.
Maybe I should prefer vector of vectors to substitude the container "matrix"?
But how could I know the dimensions of vector of vectors?
Do I have some bulit in functions could use?
//to create a matrix:
int firstDim=...,secondDim=...;
vector<vector<byte> > matrix(firstDim,vector<byte>(secondDim));
//to retrieve its dimensions:
int firstDim=matrix.size();
assert(!matrix.empty());
int secondDim=matrix[0].size();
If you require to know the dimensions even if the first dimension can be zero, it's a good idea to create a very simple wrapper class that remembers them.
Thanks a lot, maybe express edition never do the totally optimization and that is why
it is free as a commercial product.
Ublas could be quite convenient, but the speed maybe a problem?