Function to print a diamond shape

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 20 17:11:11 PDT 2014


On Tuesday, 25 March 2014 at 08:42:30 UTC, monarch_dodra wrote:
>
> Interesting. I'd have thought the "extra copy" would be an 
> overall slowdown, but I guess that's not the case.
>

I installed ubuntu 14.04 64 bit, and measured some of these 
examples using gdc, ldc and dmd on a corei3 box.  The examples 
that wouldn't build had something to do with use of 
array.replicate and range.replicate conflicting in the libraries 
for gdc and ldc builds, which were based on 2.064.2.


This is the ldc2 (0.13.0 alpha)(2.064.2) result:
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./main 
1>/dev/null
brad: time: 2107[ms]
sergei: time: 2441[ms]
jay2: time: 26[ms]
diamondShape: time: 679[ms]
printDiamond: time: 19[ms]
printDiamonde2a: time: 9[ms]
printDiamonde2b: time: 8[ms]
printDiamond3: time: 14[ms]

This is the gdc(2.064.2) result:
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./a.out 
1>/dev/null
brad: time: 3216[ms]
sergei: time: 2828[ms]
jay2: time: 26[ms]
diamondShape: time: 776[ms]
printDiamond: time: 19[ms]
printDiamonde2a: time: 13[ms]
printDiamonde2b: time: 13[ms]
printDiamond3: time: 51[ms]

This is the dmd(2.065) result:
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./main 
1>/dev/null
brad: time: 10830[ms]
sergei: time: 3480[ms]
jay2: time: 29[ms]
diamondShape: time: 2462[ms]
printDiamond: time: 23[ms]
printDiamonde2a: time: 13[ms]
printDiamonde2b: time: 10[ms]
printDiamond3: time: 23[ms]


So this printDiamonde2b example had the fastest time of the 
solutions, and had similar times on all three builds. The ldc2 
compiler build is performing best in most examples on ubuntu.

void printDiamonde2b(in uint N)
{
     uint N2 = N/2;
     char pSpace[] = uninitializedArray!(char[])(N2);
     pSpace[] = ' ';

     char pStars[] = uninitializedArray!(char[])(N+1);
     pStars[] = '*';

     pStars[$-1] = '\n';

     auto w = appender!(char[])();
     w.reserve(N*3);

     foreach (n ; 0 .. N2 + 1){
         w.put(pSpace[0 .. N2 - n]);
	w.put(pStars[$-2*n-2 .. $]);
	}

     foreach_reverse (n ; 0 .. N2){
         w.put(pSpace[0 .. N2 - n]);
	w.put(pStars[$-2*n-2 .. $]);
	}

     write(w.data);
}






More information about the Digitalmars-d-learn mailing list