Comment explaining choice of map+to_list over fold.
[cellular-automata.git] / 003 / src / life.erl
index 4f3b2e5..ddb5115 100644 (file)
@@ -61,7 +61,12 @@ life_loop(
         end
     ),
 
-    {NewTime, NewBoard} = timer:tc(fun() -> next_generation(X, Y, Board) end),
+    {NewTime, NewBoard} = timer:tc(
+        fun() ->
+            next_generation(X, Y, Board)
+        end
+    ),
+
     NewState = State#state{board        = NewBoard
                           ,gen_count    = GenCount + 1
                           ,gen_duration = NewTime
@@ -89,7 +94,10 @@ do_print_status(Bar, X, Y, N, GenCount, TimeMic, PrintTimeMic) ->
 
 
 do_print_board(Board) ->
-    CharLists = array:to_list(
+    % It seems that just doing a fold should be faster than map + to_list
+    % combo, but, after measuring several times, map + to_list has been
+    % consistently (nearly twice) faster than either foldl or foldr.
+    RowStrings = array:to_list(
         array:map(
             fun(_, Row) ->
                 array:to_list(
@@ -106,10 +114,10 @@ do_print_board(Board) ->
     ),
 
     ok = lists:foreach(
-        fun(CharList) ->
-            ok = io:format("~s~n", [CharList])
+        fun(RowString) ->
+            ok = io:format("~s~n", [RowString])
         end,
-        CharLists
+        RowStrings
     ).
 
 
This page took 0.028004 seconds and 4 git commands to generate.