给出一个小图,例如:
mygraph <-barabasi.game(5)
V(mygraph)$color <- 1:vcount(mygraph)
V(mygraph)$name <- c("one", "two", "three", "four", "five")
如果我比较这些方法来获取顶点属性:
benchmark(V(mygraph)$color[5],
V(mygraph)[5]$color,
vertex_attr(mygraph, 'color', V(mygraph)[5]),
vertex_attr(mygraph, 'color', 5),
vertex_attr(mygraph, 'color', 'five'),
replications=1000)
事实证明vertex_attr
,无论是顶点id还是名称,都能获得最快的结果:
test replications elapsed relative
4 vertex_attr(mygraph, "color", 5) 1000 0.016 1.000
5 vertex_attr(mygraph, "color", "five") 1000 0.287 17.937
3 vertex_attr(mygraph, "color", V(mygraph)[5]) 1000 1.113 69.562
2 V(mygraph)[5]$color 1000 33.111 2069.437
1 V(mygraph)$color[5] 1000 24.403 1525.187
鉴于我需要最快的方法,因为我在循环中使用它,我应该使用这种方式来访问属性吗? (我犹豫,因为这在文档中没有明确说明)