Do you have any methods to speed up the dynamic array?

Oct 13, 2010 at 12:13pm
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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.

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
Last edited on Oct 13, 2010 at 12:17pm
Oct 13, 2010 at 1:22pm
You might want to use g++, as my results are:

time of ublas : 0.160 
time of multi array : 0.035 
time of raw array : 0.036 


Particularly multi_array is considerably faster and just as performant as regular arrays.
Another alternative is to use a vector of vectors.
Last edited on Oct 13, 2010 at 1:24pm
Oct 13, 2010 at 1:42pm
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?

Thank you very much

PS:I never used g++ before
Oct 13, 2010 at 1:54pm
I ran the code you posted to get these results.

1
2
3
4
5
6
7
8
//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.
Oct 14, 2010 at 4:13am
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?
Topic archived. No new replies allowed.