Memory vs Performance?

M

Mohitz

Hi,

Need views on the following..

I need to design a data structure to store the values of n attributes,
where n is something near 20.
Now, all the attributes are optional. So, at times, i may have to
store the values of all n of them and
at times not.

I can think of the following two options.

1. Design it as a list and store the attribute name and attribute
value both. Use as much memory as needed :)
But this leads to operation overhead while accessing data.

2. Make a data structure with n attributes. Whenever, you dont have
the value for an attribute, just set it to NULL.
No performance overhead, but memory overhead.


Two questions:

1. Are there any other solutions possible that gives the best of the
two worlds?

2. If yes, what? If no, which of the above two is better??

Thanks
 
G

Gavin Deane

Hi,

Need views on the following..

I need to design a data structure to store the values of n attributes,
where n is something near 20.
Now, all the attributes are optional. So, at times, i may have to
store the values of all n of them and
at times not.

I can think of the following two options.

1. Design it as a list and store the attribute name and attribute
value both. Use as much memory as needed :)
But this leads to operation overhead while accessing data.

2. Make a data structure with n attributes. Whenever, you dont have
the value for an attribute, just set it to NULL.
No performance overhead, but memory overhead.

Two questions:

1. Are there any other solutions possible that gives the best of the
two worlds?

2. If yes, what? If no, which of the above two is better??

Whichever produces code that most clearly communicates your intent to
a human reader.

Gavin Deane
 
M

Mohitz

Why is that important??

Whichever produces code that most clearly communicates your intent to
a human reader.

Gavin Deane- Hide quoted text -

- Show quoted text -
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi,

Need views on the following..

I need to design a data structure to store the values of n attributes,
where n is something near 20.
Now, all the attributes are optional. So, at times, i may have to
store the values of all n of them and
at times not.

I can think of the following two options.

1. Design it as a list and store the attribute name and attribute
value both. Use as much memory as needed :)
But this leads to operation overhead while accessing data.

2. Make a data structure with n attributes. Whenever, you dont have
the value for an attribute, just set it to NULL.
No performance overhead, but memory overhead.

Seems like you mixed the pros and cons up, the second alternative does
not have as much memory overhead as the first one. The performance
overhead should not be so great either (just a comparison), at least
not in the case where there is an attribute.
Two questions:

1. Are there any other solutions possible that gives the best of the
two worlds?

2. If yes, what? If no, which of the above two is better??

Depending on the usage and type of values the first approach might be
a good idea. If the values tend to be large the second might be
better. How large "large" is depends on a number of things, like how
many objects you have and so on. Depending on the type of attributes
and how you access them there might be other solutions.
 
D

Devon Null

Mohitz said:
Why is that important??

First, please don't top post (order fixed).

Second, imagine you had to maintain this code -

#include <stdio.h>

main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

/* The above code is apparently from a winner of the world most
obsfucated code contest. I found it on Google.*/

Yes, I know it is C and I am using formatting as an example, but the
idea is the same - the clearer you make your intent, the easier it is
for someone else to maintain. BTW, try compiling the above program, it
works.

DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 
J

Jerry Coffin

Hi,

Need views on the following..

I need to design a data structure to store the values of n attributes,
where n is something near 20.
Now, all the attributes are optional. So, at times, i may have to
store the values of all n of them and at times not.

You've given us far too little information to provide a meaningful
answer. Questions that need to be considered:

1) What is the average value of N?
2) Are all the attributes of the same type?
3) How big are the attributes (e.g. compared to a pointer)?
4) How much do speed and memory usage matter to you?
5) For a given object, is the # of attributes constant or varying?
I can think of the following two options.

1. Design it as a list and store the attribute name and attribute
value both. Use as much memory as needed :)
But this leads to operation overhead while accessing data.

Keep in mind that a linked list also has to store pointers along with
the data -- so if N is usually close to 20, this will increase memory
usage.
2. Make a data structure with n attributes. Whenever, you dont have
the value for an attribute, just set it to NULL.
No performance overhead, but memory overhead.

This also keeps life simple of the attributes are of completely
different types, where storing heterogenous types in a collection can
make life relatively difficult.
Two questions:

1. Are there any other solutions possible that gives the best of the
two worlds?

Maybe. For example, if the attributes are fairly large, you might create
a structure containing a pointer to each attribute, with a null pointer
if the attribute isn't present.
2. If yes, what? If no, which of the above two is better??

Better for what? My immediate choice would be the second one you listed
above -- it's simple, straigtforward and fast. I'd switch to something
else only when/if profiling showed that reducing memory would make a
noticeable difference, and looking at relative sizes indicated that a
meaningful amount of space could be saved.
 

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

Forum statistics

Threads
474,292
Messages
2,571,498
Members
48,185
Latest member
abhaysingh01

Latest Threads

Top