Re: Function to print a diamond shape

2014-04-23 Thread Jay Norwood via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 15:25:04 UTC, monarch_dodra wrote: Yeah, that's because join actually works on "RoR, R", rather than "R, E". This means if you feed it a "string[], string", then it will actually iterate over individual *characters*. Not only that, but since you are using char[], it

Re: Function to print a diamond shape

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 11:41:41 UTC, Jay Norwood wrote: 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. Yeah, that's becaus

Re: Function to print a diamond shape

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 05:05:30 UTC, Jay Norwood wrote: 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 b

Re: Function to print a diamond shape

2014-04-22 Thread Jay Norwood via Digitalmars-d-learn
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@jay-ubuntu:~/ec_ddt/workspace/diamond/source$ ./main 1>/dev/null brad: time:

Re: Function to print a diamond shape

2014-04-21 Thread Jay Norwood via Digitalmars-d-learn
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' w

Re: Function to print a diamond shape

2014-04-21 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 21 April 2014 at 00:11:14 UTC, Jay Norwood wrote: 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 =

Re: Function to print a diamond shape

2014-04-20 Thread Jay Norwood via Digitalmars-d-learn
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 ex

Re: Function to print a diamond shape

2014-03-28 Thread bearophile
void main(){ import std.stdio,std.range,std.algorithm,std.conv; auto m=10.iota.map!(_=>readln.split.to!(int[])); m.map!sum.chain(m.transposed.map!sum).reduce!max.write; } It used to work, but with the latest changes I think I have broken it. Bye, bearophile

Re: Function to print a diamond shape

2014-03-25 Thread Jay Norwood
This corrects the parallel example range in the second foreach. Still slow. void printDiamonde2cpa(in uint N) { size_t N2 = N/2; char p[] = uninitializedArray!(char[])(N2+N); p[0..N2] = ' '; p[N2..$] = '*'; char nl[] = uninitializedArray!(char[])(1); nl[] = '\n'; ch

Re: Function to print a diamond shape

2014-03-25 Thread Jay Norwood
On Wednesday, 26 March 2014 at 04:47:48 UTC, Jay Norwood wrote: This is a first attempt at using parallel, but no improvement oops. scratch that one. I tested a pointer to the wrong function.

Re: Function to print a diamond shape

2014-03-25 Thread Jay Norwood
This is a first attempt at using parallel, but no improvement in speed on a corei7. It is about 3x slower than the prior versions. Probably the join was not a good idea. Also, no foreach_reverse for the parallel, so it requires extra calculations for the reverse index. void printDiamonde2

Re: Function to print a diamond shape

2014-03-25 Thread Jay Norwood
On Tuesday, 25 March 2014 at 15:31:12 UTC, monarch_dodra wrote: I love how D can achieve *great* performance, while still looking readable and maintainable. Yes, I'm pretty happy to see the appender works well. The parallel library also seems to work very well in my few experiences with it.

Re: Function to print a diamond shape

2014-03-25 Thread monarch_dodra
On Tuesday, 25 March 2014 at 12:30:37 UTC, Jay Norwood wrote: These are times on ubuntu. printDiamond3 was slower than printDiamond. Hum... Too bad :/ I was able to improve my first "printDiamon" by having a single slice that contains spaces then stars, and make writeln's of that. It gave (

Re: Function to print a diamond shape

2014-03-25 Thread Jay Norwood
These are times on ubuntu. printDiamond3 was slower than printDiamond. brad: time: 12387[ms] printDiamond1: time: 373[ms] printDiamond2: time: 722[ms] printDiamond3: time: 384[ms] jay1: time: 62[ms] sergei: time: 3918[ms] jay2: time: 28[ms] diamondShape: time: 2725[ms] printDiamond: time: 19[ms

Re: Function to print a diamond shape

2014-03-25 Thread Jay Norwood
Interesting. I'd have thought the "extra copy" would be an overall slowdown, but I guess that's not the case. I also tried your strategy of adding '\n' to the buffer, but I was getting some bad output on windows. I'm not sure why "\n\n" works though. On *nix, I'd have also expected a doub

Re: Function to print a diamond shape

2014-03-25 Thread monarch_dodra
On Tuesday, 25 March 2014 at 02:25:57 UTC, Jay Norwood wrote: not through yet with the diamond. This one is a little faster. Appending the newline to the stars and calculating the slice backward from the end would save a w.put for the newlines ... probably faster. I keep looking for a way to

Re: Function to print a diamond shape

2014-03-24 Thread Jay Norwood
These were times on ubuntu. I may have printed debug build times previously, but these are dmd release build. I gave up trying to figure out how to build ldc on ubuntu. The dmd one click installer is much appreciated. brad: time: 12425[ms] printDiamond1: time: 380[ms] printDiamond2: time: 72

Re: Function to print a diamond shape

2014-03-24 Thread Jay Norwood
not through yet with the diamond. This one is a little faster. Appending the newline to the stars and calculating the slice backward from the end would save a w.put for the newlines ... probably faster. I keep looking for a way to create a dynamic array of a specific size, filled with the in

Re: Function to print a diamond shape

2014-03-24 Thread bearophile
On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: This is a somewhat common little exercise: if you like similar puzzles, here is another: Write a program that expects a 10-by-10 matrix from standard input. The program should compute sum of each row and each column and print the

Re: Function to print a diamond shape

2014-03-24 Thread Jay Norwood
Very nice example. I'll test on ubuntu later. On windows ... D:\diamond\diamond\diamond\Release>diamond 1> nul brad: time: 19544[ms] printDiamond1: time: 1139[ms] printDiamond2: time: 1656[ms] printDiamond3: time: 663[ms] jay1: time: 455[ms] sergei: time: 11673[ms] jay2: time: 411[ms] diamondS

Re: Function to print a diamond shape

2014-03-24 Thread monarch_dodra
On Sunday, 23 March 2014 at 18:28:18 UTC, Jay Norwood wrote: On Sunday, 23 March 2014 at 17:30:20 UTC, bearophile wrote: The task didn't ask for a computationally efficient solution :-) So you are measuring something that was not optimized for. So there's lot of variance. Bye, bearophile

Re: Function to print a diamond shape

2014-03-23 Thread Jay Norwood
These were the times on ubuntu 64 bit dmd. I added diamondShape, which is slightly modified to be consistent with the others .. just removing the second parameter and doing the writeln calls within the function, as the others have been done. This is still with dmd. I've downloaded ldc. Als

Re: Function to print a diamond shape

2014-03-23 Thread Jay Norwood
On Sunday, 23 March 2014 at 17:30:20 UTC, bearophile wrote: The task didn't ask for a computationally efficient solution :-) So you are measuring something that was not optimized for. So there's lot of variance. Bye, bearophile Yes, this is just for my own education. My builds are using

Re: Function to print a diamond shape

2014-03-23 Thread bearophile
Jay Norwood: A problem with the previous brad measurement is that his solution creates a diamond of size 2n+1 for an input of n. Correcting the size input for brad's function call, and re-running, I get this. So the various solutions can have overhead computation time of 40x difference, dep

Re: Function to print a diamond shape

2014-03-23 Thread Jay Norwood
A problem with the previous brad measurement is that his solution creates a diamond of size 2n+1 for an input of n. Correcting the size input for brad's function call, and re-running, I get this. So the various solutions can have overhead computation time of 40x difference, depending on the i

Re: Function to print a diamond shape

2014-03-23 Thread Jay Norwood
I converted the solution examples to functions, wrote a test to measure each 100 times with a diamond of size 1001. These are release build times. timon's crashed so I took it out. Maybe I made a mistake copying ... have to go back and look. D:\diamond\diamond\diamond\Release>diamond 1>nul

Re: Function to print a diamond shape

2014-03-23 Thread Jay Norwood
Hmmm, looks like stderr.writefln requires format specs, else it omits the additional parameters. (not so on derr.writefln) stderr.writefln("time: %s%s",sw.peek().msecs, "[ms]"); D:\diamond\diamond\diamond\Release>diamond 1>nul time: 16[ms] time: 44[ms]

Re: Function to print a diamond shape

2014-03-23 Thread Luís.Marques
On Saturday, 22 March 2014 at 14:41:48 UTC, Jay Norwood wrote: The computation times of different methods can differ a lot. How do you suggest to measure this effectively without the overhead of the write and writeln output? Would a count of 11 and stubs like below be reasonable, or wou

Re: Function to print a diamond shape

2014-03-22 Thread Ali Çehreli
On 03/22/2014 06:03 PM, Jay Norwood wrote: > derr.writefln("time: ", sw.peek().msecs, "[ms]"); Cool. stderr should work too: stderr.writefln(/* ... */); Ali

Re: Function to print a diamond shape

2014-03-22 Thread Jay Norwood
I decided to redirect stdout to nul and print the stopwatch messages to stderr. So, basically like this. import std.stdio; import std.datetime; import std.cstream; StopWatch sw; sw.start(); measured code sw.stop(); derr.writefln("time: ", sw.peek().msecs, "[ms]"); Then, windows results compa

Re: Function to print a diamond shape

2014-03-22 Thread Jay Norwood
The computation times of different methods can differ a lot. How do you suggest to measure this effectively without the overhead of the write and writeln output? Would a count of 11 and stubs like below be reasonable, or would there be something else that would prevent the optimizer fr

Re: Function to print a diamond shape

2014-03-21 Thread Sergei Nosov
On Friday, 21 March 2014 at 13:59:27 UTC, Vladimir Panteleev wrote: On Friday, 21 March 2014 at 12:32:58 UTC, Sergei Nosov wrote: On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: This is a somewhat common little exercise: Write a function that takes the size of a diamond and produ

Re: Function to print a diamond shape

2014-03-21 Thread Vladimir Panteleev
On Friday, 21 March 2014 at 12:32:58 UTC, Sergei Nosov wrote: On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: This is a somewhat common little exercise: Write a function that takes the size of a diamond and produces a diamond of that size. When printed, here is the output for s

Re: Function to print a diamond shape

2014-03-21 Thread Andrea Fontana
On Friday, 21 March 2014 at 12:32:58 UTC, Sergei Nosov wrote: Probably, the most boring way is foreach(i; 0..N) { foreach(j; 0..N) write(" *"[i + j >= N/2 && i + j < 3*N/2 && i - j <= N/2 && j - i <= N/2]); writeln; } A single foreach(i; 0..N*N) is more boring!

Re: Function to print a diamond shape

2014-03-21 Thread Jay Norwood
This one calculates, then outputs subranges of the ba and sa char arrays. int n = 11; int blanks[]; blanks.length = n; int stars[]; stars.length = n; char ba[]; ba.length = n; ba[] = ' '; // fill full ba array char sa[]; sa.length = n; sa[] = '*'; // fill full sa array int c = n/2; // center of

Re: Function to print a diamond shape

2014-03-21 Thread Sergei Nosov
On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: This is a somewhat common little exercise: Write a function that takes the size of a diamond and produces a diamond of that size. When printed, here is the output for size 11: * *** * *** * *

Re: Function to print a diamond shape

2014-03-20 Thread Jay Norwood
On Friday, 21 March 2014 at 00:31:58 UTC, bearophile wrote: This is a somewhat common little exercise: Write a function Bye, bearophile I like that replicate but easier for me to keep track of the counts if I work from the center. int blanks[]; blanks.length = n; int stars[]; stars.length

Re: Function to print a diamond shape

2014-03-20 Thread Ali Çehreli
On 03/20/2014 02:25 PM, Ali Çehreli wrote: Write a function that takes the size of a diamond and produces a diamond of that size. I have learned a lot, especially the following two: 1) chain'ing iotas is an effective way of producing non-monotonic number intervals (and more). 2) There is s

Re: Function to print a diamond shape

2014-03-20 Thread bearophile
Ali Çehreli: This is a somewhat common little exercise: Write a function that takes the size of a diamond and produces a diamond of that size. When printed, here is the output for size 11: * *** * *** * *** * *** * *** *

Re: Function to print a diamond shape

2014-03-20 Thread Brad Anderson
On Thursday, 20 March 2014 at 22:46:53 UTC, Ali Çehreli wrote: On 03/20/2014 03:03 PM, Brad Anderson wrote: > I'm not entirely happy with it but: I am not happy with my attempt either. :) >void main() >{ > import std.algorithm, std.range, std.stdio, std.conv; > > enum length

Re: Function to print a diamond shape

2014-03-20 Thread Ali Çehreli
On 03/20/2014 03:48 PM, Timon Gehr wrote: On 03/20/2014 10:25 PM, Ali Çehreli wrote: This is a somewhat common little exercise: Write a function that takes the size of a diamond and produces a diamond of that size. When printed, here is the output for size 11: * *** * ***

Re: Function to print a diamond shape

2014-03-20 Thread Ali Çehreli
On 03/20/2014 02:52 PM, Chris Williams wrote: On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: What interesting, boring, efficient, slow, etc. ways are there? Ali Well one of the more convoluted methods that I can think of would be to define a square as a set of four vectors, ro

Re: Function to print a diamond shape

2014-03-20 Thread Timon Gehr
On 03/20/2014 10:25 PM, Ali Çehreli wrote: This is a somewhat common little exercise: Write a function that takes the size of a diamond and produces a diamond of that size. When printed, here is the output for size 11: * *** * *** * *** *

Re: Function to print a diamond shape

2014-03-20 Thread Ali Çehreli
On 03/20/2014 03:03 PM, Brad Anderson wrote: > I'm not entirely happy with it but: I am not happy with my attempt either. :) >void main() >{ > import std.algorithm, std.range, std.stdio, std.conv; > > enum length = 5; > auto rng = > chain(iota(length), iota(length

Re: Function to print a diamond shape

2014-03-20 Thread monarch_dodra
On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: What interesting, boring, efficient, slow, etc. ways are there? Ali I'd be interested in seeing a solution using "iota", and the currently proposed "each" or "tee". A quick protype to draw a triangle would be: iota(0, n).each!(

Re: Function to print a diamond shape

2014-03-20 Thread Brad Anderson
On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: This is a somewhat common little exercise: Write a function that takes the size of a diamond and produces a diamond of that size. When printed, here is the output for size 11: * *** * *** * *

Re: Function to print a diamond shape

2014-03-20 Thread Chris Williams
On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: What interesting, boring, efficient, slow, etc. ways are there? Ali Well one of the more convoluted methods that I can think of would be to define a square as a set of four vectors, rotate 45 degrees, and then create a rasterizer

Re: Function to print a diamond shape

2014-03-20 Thread Ali Çehreli
On 03/20/2014 02:30 PM, Justin Whear wrote: > What's the appropriate output for an even number? Great question! :) Size must be odd. I have this in my function: enforce(size % 2, format("Size cannot be an even number. (%s)", size)); Ali

Re: Function to print a diamond shape

2014-03-20 Thread Justin Whear
On Thu, 20 Mar 2014 14:25:02 -0700, Ali Çehreli wrote: > This is a somewhat common little exercise: Write a function that takes > the size of a diamond and produces a diamond of that size. > > When printed, here is the output for size 11: > > * > *** > * >*** > *

Function to print a diamond shape

2014-03-20 Thread Ali Çehreli
This is a somewhat common little exercise: Write a function that takes the size of a diamond and produces a diamond of that size. When printed, here is the output for size 11: * *** * *** * *** * *** * *** * What interesting