Function to print a diamond shape
Jay Norwood via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 21 22:05:28 PDT 2014
On Monday, 21 April 2014 at 08:26:49 UTC, monarch_dodra wrote:
> The two "key" points here, first, is to avoid using appender.
> Second, instead of having two buffer: " " and "******\n",
> and two do two "slice copies", to only have 1 buffer "
> *****", and to do 1 slice copy, and a single '\n' write. At
> this point, I'm not sure how we could be going any faster,
> short of using alloca...
>
> How does this hold up on your environment?
Yes your solution is the fastest yet. Also, its times are
similar for all three compilers. The range of execution times
varied for different solutions from over 108 seconds down to 64
msec.
I see that RefAppender's data() returns the managed array. Can
write() handle that? It seems that would be more efficient than
duplicating the character buffer ... or perhaps writing directly
to an OutBuffer, and then sending that to write() would avoid the
duplication?
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ gdc -O2 main.d
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./a.out
1>/dev/null
brad: time: 31865[ms]
sergei: time: 28596[ms]
jay2: time: 258[ms]
diamondShape: time: 7512[ms]
printDiamond: time: 200[ms]
printDiamonde2a: time: 140[ms]
printDiamonde2b: time: 137[ms]
printDiamond3: time: 503[ms]
printDiamonde2monarch: time: 86[ms]
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ dmd -release
main.d
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./main
1>/dev/null
brad: time: 108111[ms]
sergei: time: 33949[ms]
jay2: time: 282[ms]
diamondShape: time: 24567[ms]
printDiamond: time: 230[ms]
printDiamonde2a: time: 132[ms]
printDiamonde2b: time: 106[ms]
printDiamond3: time: 222[ms]
printDiamonde2monarch: time: 66[ms]
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ~/ldc/bin/ldc2
-O2 main.d
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./main
1>/dev/null
brad: time: 20996[ms]
sergei: time: 24841[ms]
jay2: time: 259[ms]
diamondShape: time: 6797[ms]
printDiamond: time: 194[ms]
printDiamonde2a: time: 91[ms]
printDiamonde2b: time: 87[ms]
printDiamond3: time: 145[ms]
printDiamonde2monarch: time: 64[ms]
jay at jay-ubuntu:~/ec_ddt/workspace/diamond/source$
More information about the Digitalmars-d-learn
mailing list