Why is Graph.copy() slower than copy.deepcopy(Graph) in NetworkX?

Andals :

I've been using NetworkX extensively for my research, but I've come across something that's a bit puzzling (and concerning) to me. I've been using copy.deepcopy() to copy graphs, but just realized that the Graph class has its own .copy() method which generates a deep copy.

I decided to use %timeit to see if I was handicapping myself and ended up with the following results for a graph G with 25 nodes and 66 edges:

%timeit for x in range(100): copy.deepcopy(G)
80.5 ms ± 1.26 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit for x in range(100): G.copy()
93.4 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

What is NetworkX doing differently that takes longer? This makes me concerned that I'm not doing this properly, but I haven't run into any serious issues so far that would indicate that using copy.deepcopy() is the wrong choice here.

CristiFati :

You can look at the source code yourself: [GitHub]: networkx/networkx - (master) networkx/networkx/classes/graph.py.

According to [GitHub.NetworkX]: networkx.Graph.copy (the same info is also present in the 1st URL) (emphasis is mine):

The copy method by default returns an independent shallow copy of the graph and attributes. That is, if an attribute is a container, that container is shared by the original an the copy. Use Python’s copy.deepcopy for new containers.

...

Deepcopy – A “deepcopy” copies the graph structure as well as all data attributes and any objects they might contain. The entire graph object is new so that changes in the copy do not affect the original object. (see Python’s copy.deepcopy)

...

Independent Shallow – This copy creates new independent attribute dicts and then does a shallow copy of the attributes. That is, any attributes that are containers are shared between the new graph and the original. This is exactly what dict.copy() provides.

What Graph.copy (probably) does, is additional computations in order to preserve memory.

So, if you want 2 completely independent graphs, you could use copy.deepcopy without any problems.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11847&siteId=1