From: wneumann@... Date: 2006-08-04T03:45:07+09:00 Subject: Re: For performance, write it in OCaml Jon Harrop wrote: > Here's my OCaml2 (based upon William's): Just a small note here: Depending on your goals, you can do even better by stuffing the output in a buffer then dumping the buffer at the end of computation (see example code below). Of course this consumes more memory, and isn't possible on a larger board, but it's yet another tweak to exploit, since we're benchmarking output in addition to benchmarking work (note that on my G5 output to stdout is a bit slower than your version, but this isn't the case on the AMD machines I have access to -- redirecting to a file is always faster). let rec add_a_row row = let buf = Buffer.create 1_000_000 in let rec addh row = match row with | x when x=size -> for i=0 to size-1 do String.blit output_strings.(board.(i)) 0 op (i*(size+1)) size done; Buffer.add_string buf op; Buffer.add_char buf '\n' | _ -> for latest = 0 to n - 1 do let prev_row = ref 0 in let incompat = incompat.(latest) in while !prev_row < row && not incompat.(board.(!prev_row)) do incr prev_row done; if !prev_row = row then (board.(row) <- latest; addh (row + 1)) done in addh row; Buffer.output_buffer stdout buf let () = add_a_row 0;