Function to print a diamond shape

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 22 04:41:40 PDT 2014


Wow,  joiner is much slower than join.  Such a small choice can 
make this big of a difference.  Not at all expected, since the 
lazy calls, I thought, were considered to be more efficient.  
This is with ldc2 -O2.

jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./main 
1>/dev/null
brad: time: 21958[ms]
sergei: time: 24629[ms]
jay2: time: 259[ms]
diamondShape: time: 6701[ms]
printDiamond: time: 194[ms]
printDiamonde2a: time: 95[ms]
printDiamonde2b: time: 92[ms]
printDiamond3: time: 144[ms]
printDiamonde2monarch: time: 67[ms]
printDiamonde2cJoin: time: 96[ms]
printDiamonde2cJoiner: time: 16115[ms]


void printDiamonde2cJoin(in uint N)
{
	int n,l;
     size_t N2 = N/2;
     size_t NM1 = N-1;
     char p[] = uninitializedArray!(char[])(N2+N);
     p[0..N2] = ' ';
     p[N2..$] = '*';
     char nl[] = uninitializedArray!(char[])(1);
     nl[] = '\n';

     char wc[][] = minimallyInitializedArray!(char[][])(N);

     for(n=0,l=0; n<N2; n++,l+=2){
     	wc[n] = wc[NM1-n] = p[n .. N2+l+1];
     }

     wc[N2] = p[N2..$];
     auto wj = join(wc,nl);
     write(wj);
     write('\n');
}

void printDiamonde2cJoiner(in uint N)
{
	int n,l;
     size_t N2 = N/2;
     size_t NM1 = N-1;
     char p[] = uninitializedArray!(char[])(N2+N);
     p[0..N2] = ' ';
     p[N2..$] = '*';

     char wc[][] = minimallyInitializedArray!(char[][])(N);

     for(n=0,l=0; n<N2; n++,l+=2){
     	wc[n] = wc[NM1-n] = p[n .. N2+l+1];
     }

     wc[N2] = p[N2..$];
     write(joiner(wc,"\n"));
     write('\n');
}




More information about the Digitalmars-d-learn mailing list