Is one large or a few smaller dictionaries more memory efficient?

Reply

Join Date: Mar 2009
Posts: 6
Reputation: daniwebnewbie is an unknown quantity at this point 
Solved Threads: 1
daniwebnewbie daniwebnewbie is offline Offline
Newbie Poster

Is one large or a few smaller dictionaries more memory efficient?

 
0
  #1
Mar 22nd, 2009
I'm writing an application that needs to store a ton of objects and some attributes.

I was wondering, based on python's inner workings, if it's more memory efficient to have one huge dictionary or many small dictionaries and then one dictionary that just references the smaller dictionaries?

Any information would be helpful.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 902
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Is one large or a few smaller dictionaries more memory efficient?

 
0
  #2
Mar 22nd, 2009
Personally i think if your that worried about efficiency then you should try a language like C++ which is a lot faster then python anyway.

But my bet would be on the larger one as that would take up less memory on your computer then lots of smaller ones.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,160
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 531
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is online now Online
Moderator

Re: Is one large or a few smaller dictionaries more memory efficient?

 
-7
  #3
Mar 22nd, 2009
Are you using binary trees?

For something like a dictionary thats proobably the most efficient

it halfs the search field each time it does a comparison so for lots and lots of nodes you only need a small number of compares (assuming its balanced)
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,276
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Is one large or a few smaller dictionaries more memory efficient?

 
0
  #4
Mar 22nd, 2009
A dictionary has hashed lookup and is highly optimized in Python, since the language uses dictionaries internally too. I would go with one dictionary.

C++ has the STL map<const Key, Data> container, but I don't know if that would be any memory saving, and don't think the lookup is hashed for speed.
Last edited by sneekula; Mar 22nd, 2009 at 7:22 pm.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 33
Reputation: paddy3118 is an unknown quantity at this point 
Solved Threads: 8
paddy3118 paddy3118 is offline Offline
Light Poster

Re: Is one large or a few smaller dictionaries more memory efficient?

 
1
  #5
Mar 23rd, 2009
Use whatever is easiest to write the code in.

Get it right. Profile; then see if you need to worry about optimisation.

Once you have a working program, you often find that optimisations are unnecessary, or, when you profile, you find that the bottleneck isn't where you thought it might be. This way you save wasted effort, and are more likely to have something working when your deadline looms.

- Paddy.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Is one large or a few smaller dictionaries more memory efficient?

 
1
  #6
Mar 23rd, 2009
Actually I did a bit of research a while back on how Python allocated memory. For types like lists and dictionaries, once they are created (and the space allocated), Python doesn't free the memory for the remainder of the script's lifetime, but instead holds onto it for when newer ones are created.

This would suggest that several smaller dictionaries are more efficient that one large one IF the dictionaries are not being used concurrently (ie, one is released and then another created). But for you this doesn't seem to be the case, so I would suggest that you go with one large one since it would probably be easier to manage.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,160
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 531
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is online now Online
Moderator

Re: Is one large or a few smaller dictionaries more memory efficient?

 
-7
  #7
Mar 23rd, 2009
Doesnt python have auto garbage collection?
Surely if you remove a node from the list it will thereore have nothing pointing to it, which should trigger disposal.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 902
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Is one large or a few smaller dictionaries more memory efficient?

 
0
  #8
Mar 23rd, 2009
Yeah JBennet you are exactly right.
When you remove something from a list it is unallocated from memory therefore freeing that memory up for later use.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,160
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 531
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is online now Online
Moderator

Re: Is one large or a few smaller dictionaries more memory efficient?

 
-7
  #9
Mar 23rd, 2009
Well, thats how it works in Java at least, maybe python is different.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Is one large or a few smaller dictionaries more memory efficient?

 
0
  #10
Mar 23rd, 2009
No no, you missed the point. Yes there is garbage collection, but for types like lists and dictionaries when they are garbage collected, the memory isn't actually freed, but it is kept by the interpreter (and not released back to the OS) for later use by new lists/dictionaries. The memory *is* available for use within the interpreter (so it has been garbage collected), it's just not released back to the OS.

Also note that this doesn't apply to *all* python objects (at least that's the impression I got when I read about it). Memory held by int, string, tuple objects etc. are released back to the OS when they are garbage collected.
Last edited by scru; Mar 23rd, 2009 at 7:17 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC