implementing matrices using 2D dynamic memory vs. using vectors

S

ssylee

I know how to implement a matrix as well as some of its operations
using both methods. However, b/c of my pet peeve of avoiding dealing
with dynamic memory allocations myself (at least in the beginning
stage anyway), I'm personally in favor of using vectors over using a
double pointer like **matrix while having memory allocated to it.
However, since I'm dealing with matrices with sizes up to around 4000
x 4000 elements, and execution speed is a concern for me, which would
be faster? How much more beneficial is one method over the alternative
if the execution speed would be significantly different. I know it
would depend on the hardware that's running the code as well, but in
general, I guess I can generalize the hardware that I'm running on
with at least 2 cores.

Thanks.
 
J

Jim Langston

ssylee said:
I know how to implement a matrix as well as some of its operations
using both methods. However, b/c of my pet peeve of avoiding dealing
with dynamic memory allocations myself (at least in the beginning
stage anyway), I'm personally in favor of using vectors over using a
double pointer like **matrix while having memory allocated to it.
However, since I'm dealing with matrices with sizes up to around 4000
x 4000 elements, and execution speed is a concern for me, which would
be faster? How much more beneficial is one method over the alternative
if the execution speed would be significantly different. I know it
would depend on the hardware that's running the code as well, but in
general, I guess I can generalize the hardware that I'm running on
with at least 2 cores.

The speed of vectors is almost exactly the same as the speed of doing it
yourself. However, vectors are generally safer. There are some annoyances
with dealing with a vector<vector<sometype>> such as having to allocate
memory for every column with every row (or rows with columns depending on
how you think about it). But then you have the same headache with **matrix
also.

I found one time when doing this that it was a pain, so I just made a
templace class for a two dimentional vector that would handle all this for
me. It turned out to be rather trivial but took away a lot of the headache
with using either **matrix or vector<vector<sometype>>
 
S

ssylee

The speed of vectors is almost exactly the same as the speed of doing it
yourself.  However, vectors are generally safer.  There are some annoyances
with dealing with a vector<vector<sometype>> such as having to allocate
memory for every column with every row (or rows with columns depending on
how you think about it).  But then you have the same headache with **matrix
also.

I found one time when doing this that it was a pain, so I just made a
templace class for a two dimentional vector that would handle all this for
me.  It turned out to be rather trivial but took away a lot of the headache
with using either **matrix or vector<vector<sometype>>

Do you think it would be true even for matrices with sizes up to 4000
x 4000?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top