gdc or ldc for faster programs?

Salih Dincer salihdb at hotmail.com
Sat Jan 29 18:04:11 UTC 2022


On Wednesday, 26 January 2022 at 18:00:41 UTC, Ali Çehreli wrote:
>
>
> For completeness (and noise :/) here is the final version of 
> the program:
>

Could you also try the following code with the same 
configurations?

```d
struct LongScale {
   struct ShortStack {
     short[] stack;
     size_t index;

     @property back() {
       return this.stack[0];
     }

     @property push(short data) {
       this.stack ~= data;
       this.index++;
     }

     @property pop() {
      return this.stack[--this.index];
     }
   }

   ShortStack stack;

   this(long i) {
     long s, t = i;
     for(long e = 3; e <= 18; e += 3) {
       s = 10^^e;
       stack.push = cast(short)((t % s) / (s/1000L));
       t -= t % s;
     }
     stack.push = cast(short)(t / s);
   }

   string toString() {
     string[] scale = [" zero", "thousand", "million",
     "billion", "trillion", "quadrillion", "quintillion"];
     string r;
     for(long e = 6; e > 0; e--) {
       auto t = stack.pop;
       r ~= t > 1 ? " " ~to!string(t) : t ? " one" : "";
       r ~= t ? " " ~scale[e] : "";
     }
     r ~= stack.back ? " " ~to!string(stack.back) : "";
     return r.length ? r : scale[0];
   }
}

import std.conv, std.stdio;
void main()
{
   long[] inputs = [ 741, 1_500, 2_001,
   5_005, 1_250_000, 3_000_042, 10_000_000,
   1_000_000, 2_000_000, 100_000, 200_000,
   10_000, 20_000, 1_000, 2_000, 74, 7, 0,
   1_999_999_999_999];

   foreach(long i; inputs) {
     auto OUT = LongScale(i);
     auto STR = OUT.toString[1..$];
     writefln!"%s"(STR);
   }
}
```


More information about the Digitalmars-d-learn mailing list