Skip to content

Instantly share code, notes, and snippets.

@Deepwalker
Created November 22, 2011 13:25
Show Gist options
  • Save Deepwalker/1385653 to your computer and use it in GitHub Desktop.
Save Deepwalker/1385653 to your computer and use it in GitHub Desktop.
-module(fact).
-export([fac/1, fac2/1, benchmark/1]).
fac(1) ->
1;
fac(N) ->
N * fac(N - 1).
fac2(N) ->
lists:foldl(fun(X, Prod) -> X * Prod end, 1, lists:seq(1, N)).
benchmark(N) ->
{Time1, Value1} = timer:tc(tut1, fac, [N]),
{Time2, Value2} = timer:tc(tut1, fac2, [N]),
{"Factorial of", N, "benchmarked:", Time1, "microseconds",Time2}.
{"Factorial of",70000,"benchmarked:",1902,"microseconds",
1782}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment