From: Siraaj Khandkar Date: Tue, 31 Jul 2012 20:21:13 +0000 (-0400) Subject: Logging and printing compute time for each generation. X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=c04c583261cf324ffd6a131a32e1b2c41668600a;p=cellular-automata.git Logging and printing compute time for each generation. --- diff --git a/001/src/life_observer.erl b/001/src/life_observer.erl index e16fcf7..f2d6baa 100644 --- a/001/src/life_observer.erl +++ b/001/src/life_observer.erl @@ -7,7 +7,7 @@ ,register_with_logger/0 ,add_handler/2 ,delete_handler/0 - ,log_generation/3 + ,log_generation/4 ]). %% Callbacks @@ -55,8 +55,8 @@ delete_handler() -> gen_event:delete_handler(?EVENT_MGR_REF, ?HANDLER, Args). -log_generation(GenID, Dead, Alive) -> - Event = {generation, GenID, Dead, Alive}, +log_generation(GenID, GenDuration, Dead, Alive) -> + Event = {generation, GenID, GenDuration, Dead, Alive}, gen_event:notify(?EVENT_MGR_REF, Event). @@ -88,7 +88,7 @@ init([X, Y]) -> ok = validate_makedir(file:make_dir(?PATH_DIR__DATA)), {ok, LogFile} = file:open(FilePath, write), - CSVHeaders = ["Generation", "Dead", "Alive"], + CSVHeaders = ["Generation", "Duration", "Dead", "Alive"], ok = do_write(LogFile, list_to_csvline(CSVHeaders)), {ok, #state{log_file=LogFile}}. @@ -99,10 +99,14 @@ terminate(_Reason, #state{log_file=LogFile}=State) -> {ok, State}. -handle_event({generation, GenID, Dead, Alive}, +handle_event({generation, GenID, GenDuration, Dead, Alive}, #state{log_file=LogFile}=State) -> - Values = [integer_to_list(I) || I <- [GenID, Dead, Alive]], + Values = [integer_to_list(GenID) + ,float_to_string(GenDuration) + ,integer_to_list(Dead) + ,integer_to_list(Alive) + ], ok = do_write(LogFile, list_to_csvline(Values)), {ok, State}. @@ -122,3 +126,7 @@ do_write(File, Line) -> list_to_csvline(List) -> string:join(List, ?CSV_DELIMITER). + + +float_to_string(Float) -> + io_lib:format("~f", [Float]). diff --git a/001/src/life_time.erl b/001/src/life_time.erl index 1140c79..143573d 100644 --- a/001/src/life_time.erl +++ b/001/src/life_time.erl @@ -33,6 +33,7 @@ ,state_pairs :: list(tuple(integer(), integer())) | [] ,replies_pending :: integer() ,gen_id :: integer() + ,gen_began :: erlang:timestamp() }). @@ -84,10 +85,12 @@ handle_info(next_gen, ,gen_id=GenID }=State) -> + GenBegan = os:timestamp(), NewGenID = GenID + 1, ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}), NewState = State#state{replies_pending=NumCells ,gen_id=NewGenID + ,gen_began=GenBegan ,num_dead=0 ,num_alive=0 }, @@ -105,6 +108,7 @@ handle_cast({report_state, {CellID, GenID, CellState}}, ,state_pairs=StatePairs ,replies_pending=RepliesPending ,gen_id=GenID + ,gen_began=GenBegan ,num_cells=NumCells }=State) -> @@ -124,11 +128,13 @@ handle_cast({report_state, {CellID, GenID, CellState}}, ), StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs], - ok = life_observer:log_generation(GenID, NewNDead, NewNAlive), + GenDuration = timer:now_diff(os:timestamp(), GenBegan) / 1000000, + + ok = life_observer:log_generation(GenID, GenDuration, NewNDead, NewNAlive), ok = io:format( - "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n", - [X, Y, NumCells, NewNDead, NewNAlive, GenID] + "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b DURATION: ~f~n", + [X, Y, NumCells, NewNDead, NewNAlive, GenID, GenDuration] ), ok = do_print_bar(X), ok = do_print_state_chars(X, StateChars),