have two algorithm implementations:
average(List) -> sum(List) / len(List).
sum([]) -> 0;
sum([Head | Tail]) -> Head + sum(Tail).
len([]) -> 0;
len([_ | Tail]) -> 1 + len(Tail).
average1(List) -> average_acc(List, 0,0).
average_acc([], Sum, Length) -> Sum / Length;
average_acc([H | T], Sum, Length) -> average_acc(T, Sum + H, Length + 1).
and output for trace GC events gc_start gc_end (gc started and stopped):
here every next value for process is a sum of preceding value and last gc time
average: 5189
average: 14480
average: 15118average1: 594
Why so big difference?
PS. I use wall clock time.
timestampflag) to measure what GC takes because even GC is not rescheduled in Erlang scheduler thread the thread self can be rescheduled by underlying OS. So you should usecpu_timestampinstead.average/1is usingsum/1andcount/1implementation where both are not tail recursive so you allocate2*Nstack frames which makes big difference in performance toaverage1/1which uses tail recursiveaverage_acc/3. Soaverage1/1performs exactly as loop in other languages.Edit:
timestampflag for trace messages.