0

嗨!我想获得我的函数( test(G) )的执行时间。当我使用 Timer 时,我需要编写对象的类型: "test(% ?? )" %G 这里是有向图。我怎样才能做到这一点?

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))
4

1 回答 1

1

您也应该G从中导入__main__

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

请注意,我还修复了 import 语句。

于 2010-07-26T13:39:26.867 回答