Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Ragged array whose rows are of varying size

Question posted by: er (Guest) on July 4th, 2008 06:25 PM
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 typefrom an efficiency
standpoint?

Thanks.
Puppet_Sock's Avatar
Puppet_Sock
Guest
n/a Posts
July 4th, 2008
06:45 PM
#2

Re: Ragged array whose rows are of varying size
On Jul 4, 2:25*pm, er <erwann.rog...@gmail.comwrote:
Quote:
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 typefrom 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

er's Avatar
er
Guest
n/a Posts
July 4th, 2008
06:55 PM
#3

Re: Ragged array whose rows are of varying size
On Jul 4, 2:25 pm, er <erwann.rog...@gmail.comwrote:
Quote:
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 typefrom an efficiency
standpoint?
>
Thanks.


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


er's Avatar
er
Guest
n/a Posts
July 4th, 2008
06:55 PM
#4

Re: Ragged array whose rows are of varying size
On Jul 4, 2:44 pm, Puppet_Sock <puppet_s...@hotmail.comwrote:
Quote:
On Jul 4, 2:25 pm, er <erwann.rog...@gmail.comwrote:
>
>
>
Quote:
Hi All,

>
Quote:
I have an array

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

>
Quote:
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

>
Quote:
does std::vector<vector<some type seem fine? or is any reason to
prefer
std::vector<std::shared_ptr<vector<some typefrom 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


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::vectorvs
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).





acehreli@gmail.com's Avatar
acehreli@gmail.com
Guest
n/a Posts
July 4th, 2008
07:05 PM
#5

Re: Ragged array whose rows are of varying size
On Jul 4, 11:25*am, er <erwann.rog...@gmail.comwrote:
Quote:
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.
Quote:
each of K0, K1,...,Km is occasionally changed i.e. each row is
"resized" *occasionally*


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


vector for rows is still fine.
Quote:
does *std::vector<vector<some type seem fine?


Yes.
Quote:
or is any reason to
prefer
std::vector<std::shared_ptr<vector<some typefrom an efficiency
standpoint?


The one with shared_ptr could be unnoticably slower because of the
extra indirection through the shared_ptr. The vector<vectoruses
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

er's Avatar
er
Guest
n/a Posts
July 4th, 2008
07:35 PM
#6

Re: Ragged array whose rows are of varying size
On Jul 4, 3:02 pm, acehr...@gmail.com wrote:
Quote:
On Jul 4, 11:25 am, er <erwann.rog...@gmail.comwrote:
>
Quote:
Hi All,

>
Quote:
I have an array

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

>
Quote:
m is *fixed*,

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

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

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

>
Yes.
>
Quote:
or is any reason to
prefer
std::vector<std::shared_ptr<vector<some typefrom an efficiency
standpoint?

>
The one with shared_ptr could be unnoticably slower because of the
extra indirection through the shared_ptr. The vector<vectoruses
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!

Jerry Coffin's Avatar
Jerry Coffin
Guest
n/a Posts
July 4th, 2008
07:35 PM
#7

Re: Ragged array whose rows are of varying size
In article <b19109c9-ea8d-403a-809a-35b8760a6346
@d45g2000hsc.googlegroups.com>, Join Bytes! says...
Quote:
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 typefrom 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

er's Avatar
er
Guest
n/a Posts
July 5th, 2008
12:45 AM
#8

Re: Ragged array whose rows are of varying size
On Jul 4, 3:32 pm, Jerry Coffin <jcof...@taeus.comwrote:
Quote:
In article <b19109c9-ea8d-403a-809a-35b8760a6346
@d45g2000hsc.googlegroups.com>, erwann.rog...@gmail.com says...
>
>
>
Quote:
Hi All,

>
Quote:
I have an array

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

>
Quote:
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

>
Quote:
does std::vector<vector<some type seem fine? or is any reason to
prefer
std::vector<std::shared_ptr<vector<some typefrom 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.
>
--
Later,
Jerry.
>
The universe is a figment of its own imagination.


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

Roland Pibinger's Avatar
Roland Pibinger
Guest
n/a Posts
July 5th, 2008
10:05 PM
#9

Re: Ragged array whose rows are of varying size
On Fri, 4 Jul 2008 12:02:25 -0700 (PDT), Join Bytes! wrote:
Quote:
>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.
Quote:
Quote:
>or is any reason to
>prefer
>std::vector<std::shared_ptr<vector<some typefrom an efficiency
>standpoint?

>
>The one with shared_ptr could be unnoticably slower because of the
>extra indirection through the shared_ptr. The vector<vectoruses
>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'.



--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch

 
Not the answer you were looking for? Post your question . . .
189,875 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors