Ragged array whose rows are of varying size

E

er

Hi All,

I have an array

x00,x01,x02,...,x0K0
x10,x11,x12,...,x1K1
..
..
..
xm0,xm1,xm2,...,xmKm

m is *fixed*,
each of K0, K1,...,Km is occasionally changed i.e. each row is
"resized" *occasionally*
each row is modified *often* by the transform algorithm

does std::vector<vector<some type> > seem fine? or is any reason to
prefer
std::vector<std::shared_ptr<vector<some type>> > from an efficiency
standpoint?

Thanks.
 
P

Puppet_Sock

Hi All,

I have an array

x00,x01,x02,...,x0K0
x10,x11,x12,...,x1K1
.
.
.
xm0,xm1,xm2,...,xmKm

m is *fixed*,
each of K0, K1,...,Km is occasionally changed i.e. each row is
"resized" *occasionally*
each row is modified *often* by the transform algorithm

does  std::vector<vector<some type> > seem fine? or is any reason to
prefer
std::vector<std::shared_ptr<vector<some type>> > from an efficiency
standpoint?

It is not really possible to tell from "an efficiency
standpoint" which will be superior. It will depend on
many details of such things as object size, how often
they get moved, how often you index in, how difficult
it is to make copies, how your compiler and library
version arrange things, how your platform arranges
memory and handles paging and on and on.

The usual correct answer when efficiency is an issue
(or any resource) is to work up a test case that is as
similar to your production environment and cases as
you can manage. Work it up with both possible ways of
doing things. And get out your stopwatch and do some
tests. If it is other resources, try it both ways and
see how those other resources are used.

If the difference winds up being unimportant, then do
it whichever way makes program complexity easier to
manage.
Socks
 
E

er

Hi All,

I have an array

x00,x01,x02,...,x0K0
x10,x11,x12,...,x1K1
.
.
.
xm0,xm1,xm2,...,xmKm

m is *fixed*,
each of K0, K1,...,Km is occasionally changed i.e. each row is
"resized" *occasionally*
each row is modified *often* by the transform algorithm

does std::vector<vector<some type> > seem fine? or is any reason to
prefer
std::vector<std::shared_ptr<vector<some type>> > from an efficiency
standpoint?

Thanks.

ps:
typical values: m<10
typical values for K0,...,Km: 100, 1000
 
E

er

It is not really possible to tell from "an efficiency
standpoint" which will be superior. It will depend on
many details of such things as object size, how often
they get moved, how often you index in, how difficult
it is to make copies, how your compiler and library
version arrange things, how your platform arranges
memory and handles paging and on and on.

The usual correct answer when efficiency is an issue
(or any resource) is to work up a test case that is as
similar to your production environment and cases as
you can manage. Work it up with both possible ways of
doing things. And get out your stopwatch and do some
tests. If it is other resources, try it both ways and
see how those other resources are used.

If the difference winds up being unimportant, then do
it whichever way makes program complexity easier to
manage.
Socks

sure, i agree with everything you said, and that's probably what i'll
end up doing.
however, i'd be interested to know the sort of things that come into
play in
determining the tradeoff between std::vector<std::vector> vs
std::vector<boost::shared_ptr<vector>>

for example, what happens
- when i write on array[row j]?
- when i resize array[row j]? is there any memory reallocation for the
rows that aren't j. i would think not, but i'm no expert.

ps2: the x's are scalars (say double).
 
A

acehreli

Hi All,

I have an array

x00,x01,x02,...,x0K0
x10,x11,x12,...,x1K1
.
.
.
xm0,xm1,xm2,...,xmKm

m is *fixed*,

Using a vector for the rows should be fine then (the outer vector
below), because the number of rows never change.
each of K0, K1,...,Km is occasionally changed i.e. each row is
"resized" *occasionally*

Using a vector for each row is fine too.
each row is modified *often* by the transform algorithm

vector for rows is still fine.
does  std::vector<vector<some type> > seem fine?
Yes.

or is any reason to
prefer
std::vector<std::shared_ptr<vector<some type>> > from an efficiency
standpoint?

The one with shared_ptr could be unnoticably slower because of the
extra indirection through the shared_ptr. The vector<vector> uses
indirection anyway: sizeof(vector<some_type>) should be constant
regardless of the size of the vector.

If anything, a vector of vector of shared_ptr could make a difference

vector<vector<shared_ptr<some_type> > >

if some_type is very expensive to copy or noncopyable. In that case
you may want to consider boost::ptr_vector as well:

vector<ptr_vector<some_type> >

Ali
 
E

er

Using a vector for the rows should be fine then (the outer vector
below), because the number of rows never change.


Using a vector for each row is fine too.


vector for rows is still fine.


The one with shared_ptr could be unnoticably slower because of the
extra indirection through the shared_ptr. The vector<vector> uses
indirection anyway: sizeof(vector<some_type>) should be constant
regardless of the size of the vector.

If anything, a vector of vector of shared_ptr could make a difference

vector<vector<shared_ptr<some_type> > >

if some_type is very expensive to copy or noncopyable. In that case
you may want to consider boost::ptr_vector as well:

vector<ptr_vector<some_type> >

Ali

excellent! thanks!
 
J

Jerry Coffin

Hi All,

I have an array

x00,x01,x02,...,x0K0
x10,x11,x12,...,x1K1
.
.
.
xm0,xm1,xm2,...,xmKm

m is *fixed*,
each of K0, K1,...,Km is occasionally changed i.e. each row is
"resized" *occasionally*
each row is modified *often* by the transform algorithm

does std::vector<vector<some type> > seem fine? or is any reason to
prefer
std::vector<std::shared_ptr<vector<some type>> > from an efficiency
standpoint?

Since you're not changing the number of rows, there's no particularly
good reason to use a column of pointers -- you'd do that to avoid
copying entire rows when the row vector is resized.
 
E

er

Since you're not changing the number of rows, there's no particularly
good reason to use a column of pointers -- you'd do that to avoid
copying entire rows when the row vector is resized.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Absolutely, just wanted it confirmed, just in case. thanks!
 
R

Roland Pibinger

Using a vector for the rows should be fine then (the outer vector
below), because the number of rows never change.

.... if you use reserve() before populating the vector to avoid the
unnecessary copying of elements.
The one with shared_ptr could be unnoticably slower because of the
extra indirection through the shared_ptr. The vector<vector> uses
indirection anyway: sizeof(vector<some_type>) should be constant
regardless of the size of the vector.

shared_ptr uses one dynamic allocation per element. This will slow
down the applicaton, not the 'extra indirection'.
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top