Coding Challenges - Dlang or Generic

matheus matheus at gmail.com
Tue Jan 17 23:27:03 UTC 2023


On Tuesday, 17 January 2023 at 23:08:19 UTC, Siarhei Siamashka 
wrote:
> On Tuesday, 17 January 2023 at 21:50:06 UTC, matheus wrote:
>> Question: Have you compared the timings between this way (With 
>> ranges) and a normal way (Without ranges)?
>
> If you are intensively using ranges, UFCS or the other 
> convenient high level language features, then the compiler 
> choice does matter a lot. And only LDC compiler is able to 
> produce fast binaries from such source code.
> ...

I ran in two sites: https://onecompiler.com/d and then 
https://godbolt.org/, with the latter I set LDC with -O2.

My version (Source in the end) ran about 2x faster than the 
version with ranges.

>...
>
>> I'm using D Online Compiler from different platforms but 
>> unfortunately I can't loop too much because they don't accept, 
>> but anyway a normal version runs almost twice faster than this 
>> version (With ranges).
>
> Can you try to run the following diagnostics program on this D 
> Online Compiler platform? 
> https://gist.github.com/ssvb/5c926ed9bc755900fdaac3b71a0f7cfd

https://onecompiler.com/d/3yv7t9ap9

Gives me:

HelloWorld.d(43): Error: undefined identifier `volatileStore`
HelloWorld.d(44): Error: undefined identifier `volatileLoad`


>
>> If you like I could send or post here a version without ranges 
>> to try out.
>
> Please post it. This is interesting.

Like I said above I ran my version against the one with ranges 
(From: https://github.com/quickfur/dcal/blob/master/dcal.d), and 
I modified to just print the calendar for some year.

With godbolt.org LDC -O2 my version ran 2x faster, and here is 
the source:

import std.stdio, std.string, std.conv, std.range, std.datetime;
import std.datetime.stopwatch : benchmark, StopWatch;

void draw_months(int year ,int month, int columns){
   int i;

   if(month>12){return;}

   auto columspace = " ".replicate(columns);

   write("\n");
   for(i=0;i<columns;++i){
     if(month+i>12){break;}
     write("       " ~ " ".replicate(11*(i>0))  ~ 
columspace,capitalize(to!string(Date(year,month+i,1).month)));
   }

   write("\n");
   auto m = new string[][](columns);
   for(int j=0;j<columns;++j){
     if(month+j>12){return;}
     for(i=1;i<8;++i){
         write(to!string(Date(1899,1,i).dayOfWeek)[0..2], " ");
     }

     int c = DW[to!string(Date(year,month+j,1).dayOfWeek)];
     auto dm = Date(2023,month+j,1).daysInMonth;

     for(i=0;i<c;++i){ m[j] ~= " "; }
     for(i=1;i<dm+1;++i){ m[j] ~= i.to!string; }
     for(;i<36;++i){ m[j] ~= " "; }

     write(columspace);
   }

   //write("\n\n",m[0].length,"\n");
   writeln();
   for(int k=0;k<5;++k){
     for(int j=0;j<columns;++j){
       for(i=k*7;i<(k+1)*7;++i){
         writef("%2s ",m[j][i]);
       }
       write(columspace);
     }
     writeln();
   }
}

void draw_year(int year, int columns){
   int steps = 12/columns;

   for(int i=0;i<steps;++i){
     draw_months(2023,(i*columns)+1,columns);
   }
}

int[string] DW;

void main(){
     int[string] dow;
     int i, y = 2023, m = 1, d = 1;

     auto sw = StopWatch();
     sw.start();

     for(i=1;i<8;++i){
       DW[to!string(Date(1899,1,i).dayOfWeek)]=i-1;
     }

     for(i=0;i<10;++i){
       draw_year(2023,3);
     }
     sw.stop();
     writeln(sw.peek.total!"usecs");
}

Matheus.


More information about the Digitalmars-d-learn mailing list