Created
February 11, 2009 18:29
-
-
Save brianm/62155 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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