Percolation/Bond percolation: Difference between revisions

Fixed performance problem in the D entry.
(Removed unused variable mn from C entry.)
(Fixed performance problem in the D entry.)
Line 190:
 
void initialize(in double prob, ref Xorshift rng) {
static assert(rng.front.min == 0);
immutable thresh = cast(uint)(rng.front.max * prob);
bool randP() { // Performance optimization.
immutable result = rng.front < thresh;
rng.popFront;
return result;
}
 
cells[0 .. nc] = bottomWall | rightWall; // First row.
 
Line 195 ⟶ 203:
foreach (immutable r; 1 .. nr + 1) {
foreach (immutable c; 1 .. nc)
cells[pos++] = (randP ? bottomWall : empty) |
(uniform(0.0, 1.0, rng) < prob (randP ? rightWall : empty);
cells[pos++] = rightWall | (randP ? bottomWall : empty);
empty) |
(uniform(0.0, 1.0, rng) < prob ?
rightWall :
empty);
cells[pos++] = rightWall |
(uniform(0.0, 1.0, rng) < prob ?
bottomWall :
empty);
}
 
Line 311:
p = 0.80: 0.0001
p = 0.90: 0.0000</pre>
With LDC2 compiler this code isruns in 0.26 seconds (almost threetwo times slowerfaster than the C entry).
 
=={{header|Python}}==