<div dir="ltr">Hi mail list:<div><br></div><div>I profiled my program and found the performance of tail recursion and reverse is always better than list comprehension in fprof. I feel strange after reading the discuss in mail list <<a href="http://erlang.org/pipermail/erlang-questions/2016-June/089479.html">http://erlang.org/pipermail/erlang-questions/2016-June/089479.html</a>> and wrote a small test myself.</div><div><br></div><div>The result showed that list comprehension run faster in timer:tc/3, but slower in fprof.</div><div><br></div><div>I think the result of timer:tc/3 is more believable and fprof is overestimate the cost of list comprehension. This will be misleading when I do performance optimization guided by fprof.</div><div><br></div><div>Why it happens and how to correct the result of fprof?</div><div><br></div><div><br></div><div>Best</div><div>                    Liu Zhongzheng</div><div><br></div><div>=======================================</div><div><div>6> timer:tc(tst, repeat_tail_recu, [10000000]). </div><div>{4165000,ok}</div><div>7> timer:tc(tst, repeat_list_comp, [10000000]). </div><div>{3713000,ok}</div></div><div><br></div><div><br></div><div>=======================================</div><div><div>1> fprof:apply(tst, compare, [10000]).</div><div>ok</div><div>2> fprof:profile().</div><div>Reading trace data...</div><div>End of trace!<br></div><div>ok</div><div>3> fprof:analyse().    </div></div><div>%                                               CNT       ACC       OWN        <br></div><div><div>[{ totals,                                     470633,  875.885,  860.659}].  %%%</div><div><br></div><div>{[{{error_handler,undefined_function,3},          1,  860.879,    0.003},      </div><div>  {{tst,compare,1},                            10000,    0.000,   35.380}],     </div><div> { {tst,compare,1},                            10001,  860.879,   35.383},     %</div><div> [{{tst,list_comp,1},                          10000,  457.673,   10.006},      </div><div>  {{tst,tail_recu,1},                          10000,  367.803,   10.005},      </div><div>  {suspend,                                      20,    0.020,    0.000},      </div><div>  {{tst,compare,1},                            10000,    0.000,   35.380}]}.    </div><div><br></div></div><div><br></div><div>=======================================</div><div>Here is my test code.</div><div><br></div><div><br></div><div>%% tst.erl</div><div><div>-module(tst).</div><div>-compile([export_all]).<br></div></div><div><br></div><div><div>list_comp(L) -></div><div>    [sq(I) || I <- L].</div><div><br></div><div>tail_recu(L) -></div><div>    tail_recu(L, []).</div><div><br></div><div>tail_recu([H|T], Acc) -></div><div>    tail_recu(T, [sq(H)|Acc]);</div><div>tail_recu([], Acc) -></div><div>    lists:reverse(Acc).</div><div><br></div><div>sq(X) -><br></div><div>    X*X.</div><div><br></div><div>compare(0) -><br></div><div>    ok;</div><div>compare(N) -></div><div>    L = [1,2,3,4,5,6,7,8,9,10],</div><div>    list_comp(L),</div><div>    tail_recu(L),</div><div>    compare(N-1).</div><div><br></div><div>repeat_list_comp(0) -></div><div>    ok;</div><div>repeat_list_comp(N) -></div><div>    L = [1,2,3,4,5,6,7,8,9,10],</div><div>    list_comp(L),</div><div>    repeat_list_comp(N-1).</div><div><br></div><div>repeat_tail_recu(0) -></div><div>    ok;</div><div>repeat_tail_recu(N) -></div><div>    L = [1,2,3,4,5,6,7,8,9,10],</div><div>    tail_recu(L),</div><div>    repeat_tail_recu(N-1).</div></div><div><br></div><div>% eof</div></div>