I am using NetworkX to process large graphs. I want to serialize/deserialize those graphs, and I can do that efficiently using cPickle. However, I noticed a performance penalty when I use delegation instead of inheritance, for example:
class A:
def __init__(self, **kwargs):
self.graph = Digraph()
class A(DiGraph):
def __init__(self, **kwargs):
DiGraph.__init__(self, **kwargs)
Later at some point, I use cPickle.loads to retrieve the graph and load it into memory. I benchmarked the loading times and the delegation-style graph is loaded 2 times slower than the inheritance style. Performance is an issue for me, and I was wondering why does this happen..
Delegation means you have twice the number of objects.
Without inheritance, the serialized format looks like this:
With inheritance, it’s more like: