caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andrej Bauer <Andrej.Bauer@fmf.uni-lj.si>
To: Ian Oversby <oversby@hotmail.com>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Question on writing efficient Ocaml.
Date: Fri, 29 Dec 2006 02:23:31 +0100	[thread overview]
Message-ID: <45946E13.20708@fmf.uni-lj.si> (raw)
In-Reply-To: <BAY114-F190B1A8C554A52013E63ECA9C70@phx.gbl>

[-- Attachment #1: Type: text/plain, Size: 2051 bytes --]

Dear Ian,

your ocaml code is quite "unusual", and I must say I am not convinced 
your C++ code is written optimally (but I don't know anything about C++).

I transcribed your C++ version to a reasonable ocaml version. Please 
compare what you did with how I transliterated your C++ code (attached 
file queen.ml). In particular, I use more obvious types (position is 
just int*int, a board is a 2D array)

Then I wrote my own ocaml version which mimicks your algorithm, except 
that instead of creating new boards all the time, it just keeps a list 
of current positions of queens (attached file queen2.ml).

By the way, why are you placing 8 queens on the board, no matter how 
large the board is? I thought the idea was to put n queens on a board of 
size n x n.

The results are as follows. To compute 5000 times the solution for board 
size 8:
- your C++ takes:   4.055s
- your ocaml takes: 550s
- my transliteration queen.ml of your C++ takes: 46.603s
- my improved version queen2.ml takes: 25.483s

So, for a small board size your C++ wins over ocaml by a factor of 11.5 
(or just 6.28 if I am allowed to use a reasonable representation of 
boards). Also note how my ocaml transliteration is more than 10 times 
faster than yours (you did indeed write odd code). Someone on the list 
might suggest some further improvements to queen.ml.

Since your C++ copies around a lot of boards, it loses drastically 
against the improved ocaml version when the board size goes up. For 
example, at board size 100 your C++ is 10 times slower than my ocaml 
queen2.ml, but this comparison is not fair, as I changed the data 
structure. You should implement a C++ version that uses a list of queen 
positions to represent the board and compare against queen2.ml.

Oh, by the way, C++ loses in code size (but this is well known in general):
- your C++ is 161 lines (3849 bytes)
- the ocaml transliteration of your C++ is 79 lines (1870 bytes)
- my improved ocaml version is 58 lines (1472 butes).

Feel free to post this on your blog.

Best regards,

Andrej


[-- Attachment #2: queen.ml --]
[-- Type: application/x-forcedownload, Size: 1870 bytes --]

[-- Attachment #3: queen2.ml --]
[-- Type: application/x-forcedownload, Size: 1473 bytes --]

  parent reply	other threads:[~2006-12-29  1:23 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20061203110003.6804FBC6A@yquem.inria.fr>
2006-12-28 11:42 ` Ian Oversby
2006-12-28 16:26   ` [Caml-list] " Jon Harrop
2006-12-28 17:13     ` skaller
2006-12-29  6:05       ` Jacques Garrigue
2006-12-29 11:15         ` Mattias Engdegård
2007-01-06  0:52           ` Nathaniel Gray
2007-01-06  1:01             ` Philippe Wang
2007-01-06  1:15             ` brogoff
2007-01-06  2:27               ` Martin Jambon
2007-01-08 22:23                 ` Nathaniel Gray
2006-12-28 19:24   ` Manfred Lotz
2006-12-29  1:23   ` Andrej Bauer [this message]
2006-12-29  9:58     ` [Caml-list] " Ian Oversby
2006-12-29  2:07   ` Jon Harrop
2007-01-03 16:43     ` Serge Aleynikov
     [not found] <15946.213.30.139.86.1167315231.squirrel@webmail.nerim.net>
2006-12-28 16:03 ` Ian Oversby
2006-12-28 17:00   ` Richard Jones
2006-12-28 22:23   ` Jon Harrop
2006-12-29  9:42     ` Ian Oversby

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45946E13.20708@fmf.uni-lj.si \
    --to=andrej.bauer@fmf.uni-lj.si \
    --cc=Andrej.Bauer@andrej.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=oversby@hotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).