<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Speed comparison: PLT Scheme, Ocaml and C++</title>
	<atom:link href="http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/</link>
	<description>Leveraging Perl and Emacs</description>
	<lastBuildDate>Sat, 15 Jun 2013 02:12:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Jon Harrop</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-1510</link>
		<dc:creator><![CDATA[Jon Harrop]]></dc:creator>
		<pubDate>Thu, 28 Jun 2007 13:39:05 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-1510</guid>
		<description><![CDATA[Mark, my point was that this OCaml implementation goes out of its way to box and check values at run-time. This is necessary in Lisp or Scheme but not in OCaml, Standard ML, Haskell, F#, Scala or any other modern functional programming language.]]></description>
		<content:encoded><![CDATA[<p>Mark, my point was that this OCaml implementation goes out of its way to box and check values at run-time. This is necessary in Lisp or Scheme but not in OCaml, Standard ML, Haskell, F#, Scala or any other modern functional programming language.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mork</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-343</link>
		<dc:creator><![CDATA[Mork]]></dc:creator>
		<pubDate>Wed, 07 Mar 2007 16:42:34 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-343</guid>
		<description><![CDATA[Jon: As I understand it, the point was never to write a fast implementation, but rather to compare the run-time of similar implementation approaches under different languages.
Faster code can be written in any language, but that would make the comparison between languages useless as you&#039;re not comparing similar algorithms.]]></description>
		<content:encoded><![CDATA[<p>Jon: As I understand it, the point was never to write a fast implementation, but rather to compare the run-time of similar implementation approaches under different languages.<br />
Faster code can be written in any language, but that would make the comparison between languages useless as you&#8217;re not comparing similar algorithms.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Harrop</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-155</link>
		<dc:creator><![CDATA[Jon Harrop]]></dc:creator>
		<pubDate>Sun, 31 Dec 2006 00:55:29 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-155</guid>
		<description><![CDATA[Here is a much shorter, faster and clearer implementation in OCaml:

&lt;code&gt;
open List;;

let rec safe (x1, y1) (x2, y2) =
  x1  x2 &amp;&amp; y1  y2 &amp;&amp; x2 - x1  y2 - y1 &amp;&amp; x1 - y2  x2 - y1;;

let rec search f n qs ps accu = match ps with
  &#124; [] -&gt; if length qs = n then f qs accu else accu
  &#124; q::ps -&gt; search f n qs ps (search f n (q::qs) (filter (safe q) ps) accu);;

let n = 8;;
let rec ps n x y =
  if y=n then [] else
    if x=n then ps n 0 (y + 1) else
      (x, y) :: ps n (x + 1) y;;
let ps = ps n 0 0;;
Printf.printf &quot;%d solutions\n&quot; (search (fun _ -&gt; (+) 1) n [] ps 0);;
&lt;/code&gt;

This implementation starts with a list of possible board positions and tries a queen on each one, filtering out the safe remaining positions and recursing.

If you want to print the boards, you can use:

&lt;code&gt;
let print_board n queens =
  let a = Array.make_matrix n n &#039;.&#039; in
  iter (fun (x, y) -&gt; a.(y).(x)  Array.iter print_char row; print_newline()) a;
  print_newline ();;

let () = search (fun qs () -&gt; print_board n qs) n [] ps ();;
&lt;/code&gt;

This is over 100x faster than your OCaml. It can be made much faster still by using a bitboard and bitmasks.

Cheers,
Jon.]]></description>
		<content:encoded><![CDATA[<p>Here is a much shorter, faster and clearer implementation in OCaml:</p>
<p><code><br />
open List;;</p>
<p>let rec safe (x1, y1) (x2, y2) =<br />
  x1  x2 &amp;&amp; y1  y2 &amp;&amp; x2 - x1  y2 - y1 &amp;&amp; x1 - y2  x2 - y1;;</p>
<p>let rec search f n qs ps accu = match ps with<br />
  | [] -&gt; if length qs = n then f qs accu else accu<br />
  | q::ps -&gt; search f n qs ps (search f n (q::qs) (filter (safe q) ps) accu);;</p>
<p>let n = 8;;<br />
let rec ps n x y =<br />
  if y=n then [] else<br />
    if x=n then ps n 0 (y + 1) else<br />
      (x, y) :: ps n (x + 1) y;;<br />
let ps = ps n 0 0;;<br />
Printf.printf "%d solutions\n" (search (fun _ -&gt; (+) 1) n [] ps 0);;<br />
</code></p>
<p>This implementation starts with a list of possible board positions and tries a queen on each one, filtering out the safe remaining positions and recursing.</p>
<p>If you want to print the boards, you can use:</p>
<p><code><br />
let print_board n queens =<br />
  let a = Array.make_matrix n n '.' in<br />
  iter (fun (x, y) -&gt; a.(y).(x)  Array.iter print_char row; print_newline()) a;<br />
  print_newline ();;</p>
<p>let () = search (fun qs () -&gt; print_board n qs) n [] ps ();;<br />
</code></p>
<p>This is over 100x faster than your OCaml. It can be made much faster still by using a bitboard and bitmasks.</p>
<p>Cheers,<br />
Jon.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JJ</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-114</link>
		<dc:creator><![CDATA[JJ]]></dc:creator>
		<pubDate>Sat, 23 Dec 2006 16:17:20 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-114</guid>
		<description><![CDATA[Why don&#039;t you post your question to the appropriate comp.lang.ml newsgroup, instead of using bad technology (such as blogs)?

I suppose people would help you. But you have to look in the right places for the answers you want.]]></description>
		<content:encoded><![CDATA[<p>Why don&#8217;t you post your question to the appropriate comp.lang.ml newsgroup, instead of using bad technology (such as blogs)?</p>
<p>I suppose people would help you. But you have to look in the right places for the answers you want.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IanO</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-113</link>
		<dc:creator><![CDATA[IanO]]></dc:creator>
		<pubDate>Sat, 23 Dec 2006 15:11:35 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-113</guid>
		<description><![CDATA[&gt; Neel Krishnaswami Says:
&gt; December 22nd, 2006 at 4:54 pm
&gt;
&gt; Did you compile the Ocaml to native code - that is, did you use &quot;ocamlopt&quot; instead of &quot;ocamlc&quot; or the repl? If you’re
&gt; using the interpreter, a factor of 15 or 20 relative to C++ sounds about right, actually. 

Yes I did.  Here are the commands executed - I took the arguments to the compiler from the shootout
(q.exe is the compiled ocaml, a.exe is the compiled C++).

$ ocamlopt -noassert -unsafe -ccopt -O3 -ccopt -fomit-frame-pointer q.ml -o q.exe
$ time ./q.exe
n==10
./q.exe 0.00s user 0.00s system 0% cpu 1.000 total

$ time ./a.exe 8 1000 &#124; grep -v Qu
./a.exe 8 1000 0.00s user 0.00s system 0% cpu 4.938 total

&gt; Jens Axel Søgaard Says:
&gt; December 22nd, 2006 at 5:19 pm
&gt;
&gt; Inlining a few of the functions speeds things up a bit.
&gt; With minimal changes this can be done as follows:

[snip]

This gave a speed-up of about 20% - define-inline is a nice macro, thanks.

&gt; Jens Axel Søgaard Says:
&gt; December 22nd, 2006 at 5:32 pm 
&gt; 
&gt; I know the point of the post is to compare the speed of similar approaches in different languages,
&gt; but you can save a *lot* of time by changing the representation of a board.
&gt; 
&gt; At the moment add-queen creates a new array each time a new board configuration is visited.
&gt; The representation in
&gt; 
&gt; avoids this copying, and is much faster. Try it!

Yes, much faster, you&#039;re right.  This gives a similar speed to the C++.

&gt; #  Warren Henning Says:
&gt; December 22nd, 2006 at 6:09 pm
&gt; 
&gt; Is using all that pattern matching in the OCaml code really necessary?

I&#039;ve no idea, this is my first piece of OCaml.  Do you have any suggestions for improvements?

&gt; shaurz Says:
&gt; December 22nd, 2006 at 6:26 pm
&gt; 
&gt; Are you compiling the Scheme version? Have you tried different compilers/interpreters (e.g. Chicken, Scheme 48, etc...)

No, this is running under mzscheme 352 on Windows which I believe includes a JIT.

&gt; #  smakk Says:
&gt; December 23rd, 2006 at 3:28 am
&gt; 
&gt; Can you try implementing your solution in Common Lisp? It would be interesting to see how it goes.

I have even less experience of writing CL than writing scheme and I suspect my solution would be
equally inefficient :-/]]></description>
		<content:encoded><![CDATA[<p>&gt; Neel Krishnaswami Says:<br />
&gt; December 22nd, 2006 at 4:54 pm<br />
&gt;<br />
&gt; Did you compile the Ocaml to native code &#8211; that is, did you use &#8220;ocamlopt&#8221; instead of &#8220;ocamlc&#8221; or the repl? If you’re<br />
&gt; using the interpreter, a factor of 15 or 20 relative to C++ sounds about right, actually. </p>
<p>Yes I did.  Here are the commands executed &#8211; I took the arguments to the compiler from the shootout<br />
(q.exe is the compiled ocaml, a.exe is the compiled C++).</p>
<p>$ ocamlopt -noassert -unsafe -ccopt -O3 -ccopt -fomit-frame-pointer q.ml -o q.exe<br />
$ time ./q.exe<br />
n==10<br />
./q.exe 0.00s user 0.00s system 0% cpu 1.000 total</p>
<p>$ time ./a.exe 8 1000 | grep -v Qu<br />
./a.exe 8 1000 0.00s user 0.00s system 0% cpu 4.938 total</p>
<p>&gt; Jens Axel Søgaard Says:<br />
&gt; December 22nd, 2006 at 5:19 pm<br />
&gt;<br />
&gt; Inlining a few of the functions speeds things up a bit.<br />
&gt; With minimal changes this can be done as follows:</p>
<p>[snip]</p>
<p>This gave a speed-up of about 20% &#8211; define-inline is a nice macro, thanks.</p>
<p>&gt; Jens Axel Søgaard Says:<br />
&gt; December 22nd, 2006 at 5:32 pm<br />
&gt;<br />
&gt; I know the point of the post is to compare the speed of similar approaches in different languages,<br />
&gt; but you can save a *lot* of time by changing the representation of a board.<br />
&gt;<br />
&gt; At the moment add-queen creates a new array each time a new board configuration is visited.<br />
&gt; The representation in<br />
&gt;<br />
&gt; avoids this copying, and is much faster. Try it!</p>
<p>Yes, much faster, you&#8217;re right.  This gives a similar speed to the C++.</p>
<p>&gt; #  Warren Henning Says:<br />
&gt; December 22nd, 2006 at 6:09 pm<br />
&gt;<br />
&gt; Is using all that pattern matching in the OCaml code really necessary?</p>
<p>I&#8217;ve no idea, this is my first piece of OCaml.  Do you have any suggestions for improvements?</p>
<p>&gt; shaurz Says:<br />
&gt; December 22nd, 2006 at 6:26 pm<br />
&gt;<br />
&gt; Are you compiling the Scheme version? Have you tried different compilers/interpreters (e.g. Chicken, Scheme 48, etc&#8230;)</p>
<p>No, this is running under mzscheme 352 on Windows which I believe includes a JIT.</p>
<p>&gt; #  smakk Says:<br />
&gt; December 23rd, 2006 at 3:28 am<br />
&gt;<br />
&gt; Can you try implementing your solution in Common Lisp? It would be interesting to see how it goes.</p>
<p>I have even less experience of writing CL than writing scheme and I suspect my solution would be<br />
equally inefficient :-/</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harshad Joshi</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-112</link>
		<dc:creator><![CDATA[Harshad Joshi]]></dc:creator>
		<pubDate>Sat, 23 Dec 2006 14:21:03 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-112</guid>
		<description><![CDATA[Try Scheme or Lush.]]></description>
		<content:encoded><![CDATA[<p>Try Scheme or Lush.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: smakk</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-111</link>
		<dc:creator><![CDATA[smakk]]></dc:creator>
		<pubDate>Sat, 23 Dec 2006 03:28:59 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-111</guid>
		<description><![CDATA[Can you try implementing your solution in Common Lisp? It would be interesting to see how it goes.]]></description>
		<content:encoded><![CDATA[<p>Can you try implementing your solution in Common Lisp? It would be interesting to see how it goes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jens Axel Søgaard</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-107</link>
		<dc:creator><![CDATA[Jens Axel Søgaard]]></dc:creator>
		<pubDate>Fri, 22 Dec 2006 21:04:49 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-107</guid>
		<description><![CDATA[Hi Shaurz,

Richards solution &lt;i&gt;is&lt;/i&gt; is clever. However, it only counts the number of solutions, but the three bytes ld, cols, and rd doesn&#039;t contain enough information to print out the board when a solution is found (when cols=all).

Here is a Richards solution in Scheme:

&lt;code&gt;
(define all #b11111111)

(define (queens n)
  (let ([all (- (expt 2 n) 1)])
    (let ([count 0])
      (define (try ld cols rd)
        (if (= cols all)
            (set! count (+ count 1))
            (let loop ([poss (bitwise-and all (bitwise-not (bitwise-ior ld cols rd)))])
              (unless (zero? poss)
                (let ([bit  (bitwise-and poss (- poss))])
                  (try (* 2 (bitwise-ior ld bit))
                       (bitwise-ior cols bit)
                       (quotient (bitwise-ior rd bit) 2))
                  (loop (- poss bit)))))))
      (try 0 0 0)
      (printf &quot;There was ~a solutions for a ~a board&quot; count n))))


Welcome to DrScheme, version 369.2-svn21dec2006.
Language: Pretty Big (includes MrEd and Advanced Student).
&gt; (queens 8)
There was 92 solutions for a 8 board

&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>Hi Shaurz,</p>
<p>Richards solution <i>is</i> is clever. However, it only counts the number of solutions, but the three bytes ld, cols, and rd doesn&#8217;t contain enough information to print out the board when a solution is found (when cols=all).</p>
<p>Here is a Richards solution in Scheme:</p>
<p><code><br />
(define all #b11111111)</p>
<p>(define (queens n)<br />
  (let ([all (- (expt 2 n) 1)])<br />
    (let ([count 0])<br />
      (define (try ld cols rd)<br />
        (if (= cols all)<br />
            (set! count (+ count 1))<br />
            (let loop ([poss (bitwise-and all (bitwise-not (bitwise-ior ld cols rd)))])<br />
              (unless (zero? poss)<br />
                (let ([bit  (bitwise-and poss (- poss))])<br />
                  (try (* 2 (bitwise-ior ld bit))<br />
                       (bitwise-ior cols bit)<br />
                       (quotient (bitwise-ior rd bit) 2))<br />
                  (loop (- poss bit)))))))<br />
      (try 0 0 0)<br />
      (printf "There was ~a solutions for a ~a board" count n))))</p>
<p>Welcome to DrScheme, version 369.2-svn21dec2006.<br />
Language: Pretty Big (includes MrEd and Advanced Student).<br />
&gt; (queens 8)<br />
There was 92 solutions for a 8 board</p>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shaurz</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-104</link>
		<dc:creator><![CDATA[shaurz]]></dc:creator>
		<pubDate>Fri, 22 Dec 2006 18:26:21 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-104</guid>
		<description><![CDATA[Are you compiling the Scheme version? Have you tried different compilers/interpreters (e.g. Chicken, Scheme 48, etc...)

The fastest solution I have seen the the 8-queens problem is written in MCPL by Martin Richards (designer of BCPL and MCPL). It uses clever bit-pattern tricks and only four bytes are needed to represent the board. See: http://citeseer.ist.psu.edu/richards97backtracking.html]]></description>
		<content:encoded><![CDATA[<p>Are you compiling the Scheme version? Have you tried different compilers/interpreters (e.g. Chicken, Scheme 48, etc&#8230;)</p>
<p>The fastest solution I have seen the the 8-queens problem is written in MCPL by Martin Richards (designer of BCPL and MCPL). It uses clever bit-pattern tricks and only four bytes are needed to represent the board. See: <a href="http://citeseer.ist.psu.edu/richards97backtracking.html" rel="nofollow">http://citeseer.ist.psu.edu/richards97backtracking.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren Henning</title>
		<link>http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-102</link>
		<dc:creator><![CDATA[Warren Henning]]></dc:creator>
		<pubDate>Fri, 22 Dec 2006 18:09:17 +0000</pubDate>
		<guid isPermaLink="false">http://curiousprogrammer.wordpress.com/2006/12/22/speed-comparison-plt-scheme-ocaml-and-c/#comment-102</guid>
		<description><![CDATA[Is using all that pattern matching in the OCaml code really necessary?]]></description>
		<content:encoded><![CDATA[<p>Is using all that pattern matching in the OCaml code really necessary?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
