473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

optimized handling of 2D arrays

I'm using a 3D matrix, which is actually a three 2D arrays, in a
dinamic programing that runs many times. The 2D arrays are being filled
and sometimes deallocated in each run.
I need to optimize the accessing (filling up the 2D array) part, making
it as fast as possible, while allocating is less important since it is
done less often.
Any suggestion of the fastest way to do so are welcomed.

Here is the naive approach:
F = new int**[3];
for(int i=0; i<3; ++i) {
F[i] = new int*[Rows];
for(int j=0; j < Rows; ++j) {
F[i][j]= new int[Cols];
}
}

for (int i = 1; i < Rows; ++i) {
for (int j = 1; j < Cols; ++j) {
F[0][i][j] = .... F[0][i-1][j] ....
F[1][i][j]...
...

Thanks.

Jan 26 '07 #1
2 1903
Yos P. a écrit :
I'm using a 3D matrix, which is actually a three 2D arrays, in a
dinamic programing that runs many times. The 2D arrays are being filled
and sometimes deallocated in each run.
I need to optimize the accessing (filling up the 2D array) part, making
it as fast as possible, while allocating is less important since it is
done less often.
Any suggestion of the fastest way to do so are welcomed.
Obviously, the fastest way is to use an existing matrix library (and
there are a *huge* amount of them - don't know why, perhaps it is like
the "hello world", it is the first library people think of with C++)
like blitz++ or newMat.

Overwise, an efficient approach (in your case) is to allocate the matrix
as a 1D array and then access elements by computing the index.

template <typename T>
class DumbMatrix
{
// Build array of N*M size for matrix NxM
DumbMatrix(int nbrow,int nbcol):N(nbrow),M(nbcol),matrix(N*M){}

// Constructor from initializer - If you have a fonction pattern
template<typename initializer>
DumbMatrix(int nbrow,int nbcol,initializer& matInit):N(nbrow),M(nbcol)
{
this->matrix.reserve(N*M);
for(int i=0;i<N;++i)
{
for(int(j=0;j<M;++j)
{
this->matrix.push_back(initializer(i,j));
}
}
}

//other...

//accessor
T& operator()(int i, int j){return matrix[i*M+j];

private:
int N; //number of row
int M; //number of column
std::vector<Tmatrix;
};

If you want to keep the [][] notation there has been a thread about it
recently and you can find related code. Google for "My first project" in
subject for it.

There is also the FAQ that deals with general matrix design:
http://www.parashift.com/c++-faq-lit....html#faq-15.3

Michael
Jan 26 '07 #2
Michael DOUBEZ wrote:
Yos P. a écrit :
>I'm using a 3D matrix, which is actually a three 2D arrays, in a
dinamic programing that runs many times. The 2D arrays are being filled
and sometimes deallocated in each run.
I need to optimize the accessing (filling up the 2D array) part, making
it as fast as possible, while allocating is less important since it is
done less often.
Any suggestion of the fastest way to do so are welcomed.

Obviously, the fastest way is to use an existing matrix library (and
there are a *huge* amount of them - don't know why, perhaps it is like
the "hello world", it is the first library people think of with C++)
like blitz++ or newMat.
I would add to that list Boost.uBLAS which offers the quality of code one is
used with the Boost libraries in general. You will have many options for
many types of matrices (sparse or not, bounded or not, etc).

--
Dizzy
http://dizzy.roedu.net

Jan 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Russell E. Owen | last post by:
I'm writing a C extension for numarray and am puzzled about the idiom for error handling. The documentation seems to say one should always decref an array after calling NA_InputArray, etc., to...
7
by: Matthias Czapla | last post by:
Hi! Whats the canonical way for handling raw data. I want to read a file without making any assumption about its structure and store portions of it in memory and compare ranges with constant...
2
by: aejaz | last post by:
Hey all I am new to VC++ I want to have my c++ program output to a notepad file i store my data in arrays and then use cout to output the data in DOS...as i generate data in the order of...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
12
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
4
by: Glenn | last post by:
OK, I've looked up this message but am not finding how to get rid of it: "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.