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

Below is my code:

#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()
{
  const int row = 1000;
  const int column = 1000;
  std::vector<unsigned char> sorted( 9 );
  std::clock_t begin, end;
  matrix<unsigned char> data(row, column);  

  typedef boost::numeric::ublas::zero_matrix<unsigned char> zMat;  
  matrix<unsigned char> temp( data.size1() + 2, data.size2() + 2);  
  
  int k = 0;
  begin = std::clock();
  for(unsigned int i = 0; i < data.size1(); i++)
    for(unsigned int j = 0; j < data.size2(); j++)
    {
      for( unsigned int l = 0; l< 3; l++)
        for( unsigned int 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<unsigned char, 2> dataArr(boost::extents[row][column]);
  multi_array<unsigned char, 2> tempArr(boost::extents[row + 2][column + 2]);
  int medium = 0;
  begin = std::clock();
  for(unsigned int i = 0; i < data.size1(); i++)
    for(unsigned int j = 0; j < data.size2(); j++)
    {
      for( unsigned int l = 0; l< 3; l++)
        for( unsigned int 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;
    
  unsigned char **pp;
  pp = new unsigned char* [column];
  for(unsigned int i = 0; i < column; i++)
    *(pp + i)=new unsigned char[row];    

  unsigned char **temp2 = new unsigned char *[row + 2];
  for(unsigned int i = 0; i < row + 2; ++i)
    *(temp2 + i) = new unsigned char[column + 2];
  
  int med = 0;
  begin = std::clock();
  for(unsigned int i = 0; i < column; i++)
    for(unsigned int j = 0; j < row; j++)
    {
      for( unsigned int l = 0; l< 3; l++)
        for( unsigned int 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(unsigned int i = 0; i < column; ++i)
    delete [] pp[i];
  delete [] pp;

  for(unsigned int 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.

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

>Obviously, the raw array is faster than the dynamic array of ublas and multi_array.
Not by much, and the small difference can easily be attributed to the overhead of member function calls vs. direct indexing. So ask yourself one question: Is the difference a deal-breaker for using Boost? You really shouldn't worry about optimizing unless there's a need for it and you can justify the effort involved in implementing the optimizations.

>Obviously, the raw array is faster than the dynamic array of ublas and multi_array.
Not by much, and the small difference can easily be attributed to the overhead of member function calls vs. direct indexing. So ask yourself one question: Is the difference a deal-breaker for using Boost? You really shouldn't worry about optimizing unless there's a need for it and you can justify the effort involved in implementing the optimizations.

Thanks, I try the other compiler like g++, the speed of multi_array are same as regular array, but matrix speed did not improve.
Ublas is quite convenient, but the speed is a problem?

Performance is application-specific. If the library is too slow for your application, ditch it. It's really that simple, but I strongly suspect you're worrying about nothing. Unless you can prove that there's a noticeable and truly detrimental performance hit, spending time looking for ways to squeeze out more speed is unproductive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.