0
哪一種方式在內存管理和計算速度方面最有效?Networkx:節點作爲對象或節點作爲標識符與詞典屬性表
下面的簡單測試表明,將節點中的屬性存儲爲python對象或通過屬性表進行字典查找會稍微好一些。由於內存如何分配,這是否總是這樣?
作爲測試我構造的簡單的例子:
class country():
def __init__(self, name, gdp):
self.name = name
self.gdp = gdp
def __repr__(self):
return str(self.name)
#Country Objects
countries = dict()
countries['AUS'] = country('AUS', 2000)
countries['USA'] = country('USA', 10000)
countries['ZWE'] = country('ZWE', 13)
#Attribute Dictionary
gdp = dict()
gdp['AUS'] = 2000
gdp['USA'] = 10000
gdp['ZWE'] = 13
構建網絡:
#Nodes as ID's
G1 = nx.Graph()
G1.add_nodes_from(countries.keys())
G1.nodes()
#Nodes as Objects
G2 = nx.Graph()
for c in countries.keys():
G2.add_node(countries[c])
G2.nodes()
運行%timeit在IPython的:
G1F()
#Lookup Data from Strings Network
def G1f():
for n in G1.nodes():
print "Node: %s" % n
print "\tGDP: %s" % gdp[n]
%timeit G1f
輸出爲G1F():
10000000 loops, best of 3: 26.4 ns per loop
G2F()
#Lookup Data from Objects
def G2f():
for n in G2.nodes():
print "Node: %s" % n.name
print "\tGDP: %s" % n.gdp
%timeit G2f
輸出,用於G2F()
10000000 loops, best of 3: 21.8 ns per loop
更新
G3F()從答案]
G3 = nx.Graph()
for c,v in gdp.items():
G3.add_node(c, gdp=v)
def G3f():
for n,d in G3.nodes(data=True):
print "Node: %s" % n
print "\tGDP: %s" % d['gdp']
輸出爲G13f():
10000 loops, best of 3: 63 µs per loop
謝謝 - 我對計時很感興趣,因爲我還想給邊添加屬性,並且邊的數量可能變得非常大。我將爲上面的G3f()添加%timeit作爲另一個選項。 – sanguineturtle