Repository navigation
Performance improvement and tests for edges_equal - #8077
Conversation
082ef99 to
7834679
Compare
7834679 to
d2fd929
Compare
| return False | ||
| return True | ||
| return all( | ||
| n in d2 and nbr in d2[n] and datalist.count(data) == d2[n][nbr].count(data) |
There was a problem hiding this comment.
Don't you need to check the len of data lists matches too?
There was a problem hiding this comment.
This situation is what the comment on L#552 refers to.
Afaik (but this took some testing and thinking) the only situation where datalist has a length greater than 1 is when comparing the edges of MultiGraphs with keys=False, data=True - but in that situation, the length check in the for loop catches the discrepancy.
If you have a weird situation where the difference in lengths because of one edge would be countered by a reversed difference in another edge, then the all check will fail (in that direction) on the other edge.
A concrete example would be:
G = nx.path_graph(3, create_using=nx.MultiGraph)
I = nx.path_graph(3, create_using=nx.MultiGraph)
G.add_edge(0, 1, 0, attr1="blue")
G.add_edge(1, 2, 1, attr2="green")
I.add_edge(0, 1, 0, attr1="blue")
I.add_edge(0, 1, 1, attr2="green")
edges_equal(G.edges(data=True), I.edges(data=True))Both of these graphs have 3 edges, so the length check passes. When comparing the attribute dicts, (0, 1) passes (you could say erroneously), since we are only looking at the attributes in d1 which refers to G. However, (1, 2) doesn't - because now there are 2 attributes to check for G where I only has one.
There was a problem hiding this comment.
Unless you meant "after changing the way data is parsed in the for loop", for which I've added a new test as well.
rossbar
left a comment
There was a problem hiding this comment.
To me the stronger motivation here isn't necessarily anything to do with performance but rather the improvement in readability, at least related to _update_dict.
I personally find the triply-nested comprehension harder to follow than the original loop and would advocate for undoing that particular change, but pulling the repeated logic for constructing the dicts strikes me as an improvement!
890d26c to
d5a9827
Compare
|
Since we seem to have settled on going for readability over performance, I've gone with Ale's suggestion as well. The comprehension should now be a lot more readable. :) |
| d[u, v] = data | ||
| d[v, u] = data |
There was a problem hiding this comment.
Putting the data in both edge directions means this doesn't work for directed graphs, right? If that's the case, I feel it's worth mentioning somewhere.
Does anyone have an explanation for why the original function was written that way?
There was a problem hiding this comment.
Putting the data in both edge directions means this doesn't work for directed graphs, right?
Yes I think that's right... whether or not it's intentional I don't know. The docstring does say:
The order of the edges is not relevant.
However it is a bit of a jump for the casual reader/user to go from that sentence to the fact that directedness of edges is ignored.
IMO this is worth a new issue for further discussion!
rossbar
left a comment
There was a problem hiding this comment.
This LGTM - the encapsulation of the dict creation into an internal function improves code reuse, and zip_longest is a slick way of getting rid of some of the explicit membership checks when these dicts are eventually compared. The end result is more readable IMO - thanks @Peiffap !
Co-authored-by: Dan Schult <[email protected]>
* Performance improvement and tests for `edges_equal` * Address code review comments * Clean up docstring * Remove triply-nested comprehension * Rename tuple * refactor: use new syntax Co-authored-by: Dan Schult <[email protected]> --------- Co-authored-by: Dan Schult <[email protected]>
A little maintenance PR while reading over the functions in the
utils/folder.Mini benchmark.
Benchmarking these changes shows something like a 40-50% time reduction for the complete graph in the timing code.
K_1000vsK_1000:K_1000vsK_1000with one edge removed:When comparing the complete graph to a much smaller graph, short-circuiting on the length gives an even more significant improvement:
K_1000vsK_10: