在内存管理和计算速度方面哪个最有效?
下面的简单测试表明,将节点内的属性存储为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()
在IPython中运行%timeit:
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
答案 0 :(得分:0)
你也可以使用这样的节点属性:
import networkx as nx
#Attribute Dictionary
gdp = dict()
gdp['AUS'] = 2000
gdp['USA'] = 10000
gdp['ZWE'] = 13
G3 = nx.Graph()
for c,v in gdp.items():
G3.add_node(c, gdp=v)
print G3.nodes(data=True)
def G3f():
for n,d in G3.nodes(data=True):
print "Node: %s" % n
print "\tGDP: %s" % d['gdp']
我不清楚测试时间是非常重要的。除非这是一个非常大的问题(也许有一天每个人都会拥有自己的国家!)速度或内存可能没有太大差异。我怀疑创建许多小型自定义对象(country())的开销最终会占用更多内存和时间。