views:

17

answers:

1

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))
A: 

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.

gnibbler
Thanks! That's weird because I had to also fixed import statement to make it work. But what's the difference between "from networkx import nx" and "import networkx as nx" ??
masti
`from networkx import nx` means that the module `networkx` should _contain_ a function `nx`. But you don't want that, you want to call the module _itself_ `nx`.
katrielalex