By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Memory vs Performance?

P: n/a
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

Jun 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 12 Jun, 10:03, Mohitz <coolmoh...@gmail.comwrote:
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

Jun 12 '07 #2

P: n/a
Why is that important??

On Jun 12, 6:32 pm, Gavin Deane <deane_ga...@hotmail.comwrote:
On 12 Jun, 10:03, Mohitz <coolmoh...@gmail.comwrote:


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- Hide quoted text -

- Show quoted text -

Jun 12 '07 #3

P: n/a
On 12 Jun, 10:33, Mohitz <coolmoh...@gmail.comwrote:
Why is that important??

On Jun 12, 6:32 pm, Gavin Deane <deane_ga...@hotmail.comwrote:
On 12 Jun, 10:03, Mohitz <coolmoh...@gmail.comwrote:
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.
What do you think source code is for if not communicating with other
people?

Gavin Deane

Jun 12 '07 #4

P: n/a
On 12 Juni, 11:03, Mohitz <coolmoh...@gmail.comwrote:
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.

--
Erik Wikström

Jun 12 '07 #5

P: n/a
Mohitz wrote:
>>On Jun 12, 6:32 pm, Gavin Deane <deane_ga...@hotmail.comwrote:

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

Gavin Deane
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.
Jun 12 '07 #6

P: n/a
In article <11**********************@r19g2000prf.googlegroups .com>,
co********@gmail.com says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.