Skip to content

Instantly share code, notes, and snippets.

@brianm
Created February 11, 2009 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianm/62155 to your computer and use it in GitHub Desktop.
Save brianm/62155 to your computer and use it in GitHub Desktop.
open Printf
let i = int_of_string Sys.argv.(1);;
let a = ref 0 in
for j = 1 to i do
a := (!a + j)
done;
printf "%i\n" !a;;
open Printf
let rec foo x a =
if x == 0 then a else foo (x -1) (a + x );;
let i = int_of_string Sys.argv.(1);;
let a = foo i 0;;
printf "%i\n" a;;
brianm@binky:~$ ocamlc -o recursive ./recursive.ml
brianm@binky:~$ ocamlc -o iterative ./iterative.ml
brianm@binky:~$ time ./recursive 10000000
143223616
real 0m0.227s
user 0m0.224s
sys 0m0.003s
brianm@binky:~$ time ./recursive 100000000
987459712
real 0m2.227s
user 0m2.221s
sys 0m0.005s
brianm@binky:~$ time ./iterative 100000000
987459712
real 0m2.346s
user 0m2.337s
sys 0m0.006s
brianm@binky:~$ time ./iterative 100000000
987459712
real 0m2.347s
user 0m2.336s
sys 0m0.006s
brianm@binky:~$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment