/mobile Handheld Friendly website

 performance measurements

Each table row shows performance measurements for this OCaml program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
100.570.58760473  0% 0% 3% 100%
116.826.82760473  0% 1% 0% 100%
1290.3890.41760473  0% 0% 0% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read fannkuch-redux benchmark to see what this program should do.

 notes

The OCaml native-code compiler, version 4.01.0

 fannkuch-redux OCaml #2 program source code

(* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   contributed by Ethan Burns
*)


(** Flip the front [n] pancakes of [a]. *)
let flip n (a : int array) =
  for i = 0 to n / 2 do
    let t = a.(i) in
    let k = n - i in
      a.(i) <- a.(k);
      a.(k) <- t;
  done

(** Count the number of flips so that pancake 0 is at index 0. *)
let rec count c ary =
  let z = ary.(0) in
    if z <> 0 then begin
      flip z ary;
      count (c + 1) ary
    end else
      c

(** Rotate the first [n] pancakes of [a]. *)
let rotate n (a : int array) =
  let t = a.(0) in
  let m = n - 1 in
    for i = 1 to m do
      a.(i - 1) <- a.(i);
    done;
    a.(m) <- t

(** Call [f] on each permutation of [n] numbers in order. *)
let iter_perms n f =
  let rec do_iter num perm copy f ht =
    if ht = 1 then begin
      for i = 0 to n - 1 do copy.(i) <- perm.(i) done;
      f !num copy;
      incr num;
    end else
      for i = 1 to ht do
	do_iter num perm copy f (ht - 1);
	rotate ht perm;
      done
  in
  let perm = Array.init n (fun i -> i) in
  let copy = Array.create n 0 in
  let num = ref 0 in
    do_iter num perm copy f n

let _ =
  let n = int_of_string Sys.argv.(1) in
  let csum = ref 0 and m = ref 0 in
    iter_perms n (fun num a ->
		    let c = count 0 a in
		      (* csum update from Otto Bommer's Scala ver. *)
		      csum := !csum + c * (1 - (num land 1) lsl 1);
		      if c > !m then m := c;);
    Printf.printf "%d\nPfannkuchen(%d) = %d\n" !csum n !m

 make, command-line, and program output logs

Fri, 13 Sep 2013 04:18:25 GMT

MAKE:
mv fannkuchredux.ocaml-2.ocaml fannkuchredux.ocaml-2.ml
/usr/local/bin/ocamlopt -noassert -unsafe -fno-PIC -nodynlink -inline 100 -fno-PIC unix.cmxa fannkuchredux.ocaml-2.ml -o fannkuchredux.ocaml-2.ocaml_run
File "fannkuchredux.ocaml-2.ml", line 1:
Warning 24: bad source file name: "Fannkuchredux.ocaml-2" is not a valid module name.
rm fannkuchredux.ocaml-2.ml
0.21s to complete and log all make actions

COMMAND LINE:
./fannkuchredux.ocaml-2.ocaml_run 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65

Revised BSD license

  Home   Conclusions   License   Play