Search code examples
pythondictionarymemorystructuretrie

is there any difference regarding the saving memory between a normal dictionary and a trie dictionary?


is there any difference regarding saving memory space between a trie and a normal dictionary ? if yes, is there any way to count and measure their difference ? like how many bites or bytes is their difference.

here is an example of these two types of dictionaries :

trie_dict = {'f': {'o': {'o': {'t': {'value':'to walk with'},                                            
                               'l': {'value':'to teach'},
                               'd':{'value': 'to eat'}}}}}


normal_dict =  {'foot': {'value': 'to walk with'},
                'fool': {'value': 'to teach'},
                'food': {'value': 'to eat'}}

Solution

  • This is my attempt at a better evaluation of the total size of an object (dict and str only):

    def rec_size_of(obj):
        current = 0
        if isinstance(obj, dict):
            current += getsizeof(obj)
            for k,v in obj.items():
                current += getsizeof(k)
                current += rec_size_of(v)
        elif isinstance(obj, str):
            current += getsizeof(obj)
        return current
    
    rec_size_of(trie_dict)       
    2315
    
    rec_size_of(normal_dict)         
    1454