Permalink
Browse files

Big speedup to betweenness() using Appender instead of array appending.

  • Loading branch information...
1 parent 8bff8e9 commit 5dda197a41e7d625db461ae9ea4c3bdc27c0abdd @WebDrake committed Aug 30, 2013
Showing with 8 additions and 8 deletions.
  1. +8 −8 dgraph/metric.d
View
@@ -80,7 +80,7 @@ auto betweenness(T = double, bool directed)(ref Graph!directed g, bool[] ignore
T[] delta = new T[g.vertexCount];
long[] d = new long[g.vertexCount];
auto q = VertexQueue(g.vertexCount);
- size_t[][] p = new size_t[][g.vertexCount];
+ Appender!(size_t[])[] p = new Appender!(size_t[])[g.vertexCount];
sigma[] = to!T(0);
delta[] = to!T(0);
@@ -118,8 +118,8 @@ auto betweenness(T = double, bool directed)(ref Graph!directed g, bool[] ignore
d[w] = d[v] + 1L;
assert(sigma[w] == to!T(0));
sigma[w] = sigma[v];
- p[w].length = 1;
- p[w][0] = v;
+ p[w].clear;
+ p[w].put(v);
}
else if (d[w] > (d[v] + 1L))
{
@@ -130,22 +130,22 @@ auto betweenness(T = double, bool directed)(ref Graph!directed g, bool[] ignore
be ready for that update. */
d[w] = d[v] + 1L;
sigma[w] = sigma[v];
- p[w].length = 1;
- p[w][0] = v;
+ p[w].clear;
+ p[w].put(v);
}
else if (d[w] == (d[v] + 1L))
{
sigma[w] += sigma[v];
- p[w] ~= v;
+ p[w].put(v);
}
}
}
- while(stackLength > to!size_t(0))
+ while (stackLength > to!size_t(0))
{
--stackLength;
auto w = stack[stackLength];
- foreach (immutable v; p[w])
+ foreach (immutable v; p[w].data)
{
delta[v] += ((sigma[v] / sigma[w]) * (to!T(1) + delta[w]));
}

0 comments on commit 5dda197

Please sign in to comment.