Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Comments welcomed to progr. style / speed of my first ocaml program "Error correcting LDPC decoder"
Date: Fri, 16 Dec 2005 19:35:50 +0000	[thread overview]
Message-ID: <200512161935.50714.jon@ffconsultancy.com> (raw)
In-Reply-To: <OFB50A01B3.ECDA5A94-ONC12570D8.00394FEA-C12570D8.003A2427@philips.com>

On Thursday 15 December 2005 10:33, Andries Hekstra wrote:
> I am an applied scientist in the areas of signal processing and error
> correction at Philips Research. Having done the programming work of my
> computer simulations in C/C++ for almost 10-15 years with increasingly
> mixed feelings about the languange, a friend pointed me to functional
> programming.

You may be interested in my book Objective CAML for Scientists:

  http://www.ffconsultancy.com/products/ocaml_for_scientists

It covers everything you need and is written for people with similar 
backgrounds to your own.

> I welcome comments w.r.t. programming style, esp. when it affects the speed
> of the program.

You don't need to use ";;" in compiled source.

You can replace:

  print_string "LDPC decoder for rate 1 - ";
  print_int ldpc_j;
  print_string "/";
  print_int ldpc_k;
  print_string " Gallager code with length ";
  print_int codewordLength;
  flush stdout

with a call to "Printf.printf".

You can replace:

  fun j -> fun i -> fun x -> 

with

  fun j i x ->

Try to avoid superfluous parentheses:

        if (i < j) then begin

Use Array.sort instead of writing your own "qsort". Use an array of 2-tuples 
(pairs) instead of two arrays and a custom "qsort2" function.

Put a space either side of a bracketed prefix operator, to avoid problems with 
(*):

  qsort (<) a.(i)

Coalesce computations into a single "let () = ...", rather than spreading them 
out over the source.

Spread your code out more:

  let sum a = let rec sum i a = if (i > 0) then a.(i) +. sum (i-1) a else if 
(i = 0) then a.(i) else 0.
      in sum ((Array.length a) - 1) a

  let sum a =
    let rec sum i a =
      if i > 0 then a.(i) +. sum (i-1) a else
        if i = 0 then a.(i) else 0. in
    sum (Array.length a - 1) a

It looks like you might be creating several arrays that are only filled in at 
the end, in which case it is probably better to create them at the end of a 
calculating using something like Array.init.

Your code often has similar looking parts. You can probably factor out a lot 
of common functionality into higher-order functions, reducing code size and 
the number of mistakes at the cost of a little performance due to the 
genericity of the resulting code.

Especially with long variable names, you may be able to shrink your code by 
nesting more. For example, to avoid passing parameters across many function 
calls. This can also conflict with performance but for more subtle reasons.

Use "&&" and "||" rather than "&" and "or".

You often write:

  let x=1 in begin
    for i=1 to 10 do
      ...
    done
  end

when you could write:

  let x=1 in
  for i=1 to 10 do
    ...
  done

Reducing maxNumberOfIterations to 1, the final time given is 78.54s on my 
900MHz 32-bit Athlon and 55.67s on my 800MHz AMD64.

You can shave 2% off the time by factoring the computation of pi from 
randomGaussian. You can reduce the run-time by 25% by using a faster 
randomGaussian function (e.g. the one given in NR) and compiling with 
"-inline 100".

HTH.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


      parent reply	other threads:[~2005-12-16 19:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-15 10:33 Andries Hekstra
     [not found] ` <OFB50A01B3.ECDA5A94-ONC12570D8.00394FEA-C12570D8.003A2427@philips.com >
2005-12-15 12:03   ` [Caml-list] " Gerd Stolpmann
2005-12-15 12:40     ` Gerd Stolpmann
2005-12-15 15:09       ` Xavier Leroy
2005-12-16 19:35 ` Jon Harrop [this message]

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=200512161935.50714.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.inria.fr \
    /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