From: William James Date: 2007-11-29T00:25:03+09:00 Subject: Re: Most elegant way to do this? On Nov 28, 7:10 am, Lloyd Linklater wrote: > William James wrote: > > That C-code is hideous. Let's compare it to something > > that isn't insanely low-level. > > OOOOO! I know this one! > > If you meant this as pascal, this is the code: > > function roll( n: integer ): integer; > var i: integer; > begin > for i := 1 to n do result := inc(result, random(6) + 1); > end; > > There is no += in pascal. If only... :) Your version where you init > the result so you do not have to add may run faster but I golfed it a > bit. Still, it is far better than the C version. I liked seeing it > here too. :) Glad you liked it. It strikes me as bizarre that so many Ruby users who want more speed descend from the dizzying heights of Ruby to the chthonic depths of ultra primitive C. Why not use Pascal? The version of Pascal that I am using does have +=. It's Free Pascal. Currently it's #3 in the shootout. (http://shootout.alioth.debian.org/gp4/benchmark.php? test=all&lang=all) The program below isn't as fast as I expected; random() may be a bit slow. {$mode objfpc} // So that "result :=" can be used. uses sysutils; // for timing function roll( n: integer ): integer; var i: integer; begin result := n; for i := 1 to n do result += random(6) end; var i: integer; when: tdatetime; begin randomize; when := time; for i := 1 to 3999000 do roll(2); writeln( ((time-when)*secsperday):0:2 ) end.