<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>