开发者

TypeError when iterate over DiGraph()

Hii! I want to get the time execution of my function ( test(G) ). when I use Timer I need to write the type of my object : "test(% ?? )" %G which is DiGraph here. How 开发者_开发百科can I do that?

from networkx import nx

def test(G):
    for e in G.edges_iter():
        print(e)

if __name__=='__main__':
    from timeit import Timer
    G = nx.DiGraph()
    G.add_edges_from([(1,2),(4,5)])
    t = Timer("test(% ?? )"%G,"from __main__ import test")
    print( t.timeit(1))


You should import G from __main__ as well

import networkx as nx

def test(G):
    for e in G.edges_iter():
        print(e)

if __name__=='__main__':
    from timeit import Timer
    G = nx.DiGraph()
    G.add_edges_from([(1,2),(4,5)])
    t = Timer("test(G)","from __main__ import test,G")
    print( t.timeit(1))

Note that I fixed the import statement also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜