A significant performance difference

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 1 01:22:57 PDT 2014


On Mon, 1 Sep 2014 09:21:03 +0200
Daniel Kozak via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:

and this version executes in 0.2 seconds:

    import core.stdc.stdio;

    immutable H = 9, W = 12;

    immutable uint[3][6] g = [
      [7, 0, H - 3],
      [1 + (1 << H) + (1 << (2 * H)), 0, H - 1],
      [3 + (1 << H), 0, H - 2],
      [3 + (2 << H), 0, H - 2],
      [1 + (1 << H) + (2 << H), 0, H - 2],
      [1 + (1 << H) + (1 << (H - 1)), 1, H - 1]
    ];


    int main () {
      ulong p, i, k;
      uint j, l;
      ulong[uint] x, y;
      x[0] = 1;

      for (i = 0; i < W; ++i) {
        y = null;
        while (x.length) {
          foreach (key; x.keys) {
            auto pp = key in x;
            if (pp is null) continue;
            j = key;
            p = *pp;
            x.remove(j);

            for (k = 0; k < H; ++k) {
              if ((j & (1 << k)) == 0) break;
            }

            if (k == H) {
              y[j >> H] += p;
            } else {
              for (l = 0; l < 6; ++l) {
                if (k >= g[l][1] && k <= g[l][2]) {
                  if ((j & (g[l][0] << k)) == 0) {
                    x[j + (g[l][0] << k)] += p;
                  }
                }
              }
            }
          }
        }
        x = y;
      }

      printf("%lld\n", y[0]);
      return 0;
    }

so yes, creating delegates are SLOW. even taking into account
that we creating dynamic arrays with keys in this version, it's rockin'
fast.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140901/ef99869a/attachment.sig>


More information about the Digitalmars-d-learn mailing list