A little of coordination for Rosettacode

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 31 04:29:48 PDT 2014


safety0ff:

> I modified the Hamming numbers code in a personal exercise.
> It now uses considerably less memory but is slower.
>
> I've posted the code here in case it is of use: 
> http://dpaste.dzfl.pl/3990023e5577
>
> For a single n, n = 350_000_000:
> Alternative version 2: 13.4s and ~5480 MB of ram
> My code: 21s and ~74 MB of ram

I have added your version, with small changes and improvements.


I suggest to not put attributes like this:


     static struct Candidate
     {
         typeof(Hamming.ln) ln;
         typeof(Hamming.e) e;
         pure nothrow:
         void increment(size_t n)
         {
             e[n] += 1;
             ln += lnprimes[n];
         }
         const:
         bool opEquals(T)(in ref T y) {
             // return this.e == y.e; // slow
             return !((this.e[0] ^ y.e[0]) | (this.e[1] ^ y.e[1]) 
| (this.e[2] ^ y.e[2]));
         }
         int opCmp(T)(in ref T y) {
             return ln > y.ln ? 1 : (ln < y.ln ? -1 : 0);
         }
     }

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list