<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 28, 2016 at 8:22 AM, Khitai Pang <span dir="ltr"><<a href="mailto:khitai.pang@outlook.com" target="_blank">khitai.pang@outlook.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">For string concatenation, which one of the following is the most efficient?</blockquote></div><br></div><div class="gmail_extra">Ask eministat, but note that the A ++ B ++ C is probably recognized by the compiler as dead code, so we might be checking a loop here. Another note is that a single invocation has no measurable difference, so unless you are doing millions of these, it doesn't matter.<br><br></div><div class="gmail_extra">Code and output follows. The ++ variant is fastest followed by the lists:append followed by the string:concat. But there is a lot of outlier variance in these measurements which suggest it is highly affected by outside factors. Don't put too much into these numbers and their precision as there is a lot of variance in runtime.<br><br></div><div class="gmail_extra">-----<br>-module(concat).<br><br>-export([t/0, datasets/0]).<br><br>c1(Items) -> c1(Items, 10000).<br><br>c1(_, 0) -> ok;<br>c1(Items, K) -><br> string:join(Items, ""),<br> c1(Items, K-1).<br><br>c2(Items) -> c2(Items, 10000).<br><br>c2(_, 0) -> ok;<br>c2(Items, K) -><br> lists:append(Items),<br> c2(Items, K-1).<br><br>c3(Items) -> c3(Items, 10000).<br><br>c3(_, 0) -> ok;<br>c3([A,B,C] = Is, K) -> <br> _ = A ++ B ++ C,<br> c3(Is, K-1).<br><br>datasets() -><br> ItemID = "123134-123-12313-1--123-1231-",<br> Input = ["Item:{", ItemID, "}"],<br> [eministat:s("++",<br> fun() -><br> c3(Input)<br> end, 50),<br> eministat:s("strings:join/1",<br> fun() -><br> c1(Input)<br> end, 50),<br> eministat:s("lists:append",<br> fun() -><br> c2(Input)<br> end, 50)].<br><br>t() -><br> [H | T] = datasets(),<br> eministat:x(95.0, H, T).<br>--------------<br><br>18> concat:t().<br>x ++<br>+ strings:join/1<br>* lists:append<br>+--------------------------------------------------------------------------+<br>|x xx x xxxxx+++****x *+ ***x +** + +*+ ++ +++++++++++ xx + +|<br>| x xx xxxx*** * x **** * + + + + ++++ |<br>| x x xxxx*** * **** + ++++ |<br>| x xxx *** *** + ++++ |<br>| x xxx * * *** + +++ |<br>| x x * * +++ |<br>| x * + + |<br>| x * + + |<br>| x * + |<br>| |_________MA__________| |<br>| |______A_M____| |<br>| |______AM_____| |<br>+--------------------------------------------------------------------------+<br>------<br><br>Dataset: x N=50 CI=95.0000<br>Statistic Value [ Bias] (Bootstrapped LB‥UB)<br>Min: 1404.00<br>1st Qu. 1730.00<br>Median: 1803.00<br>3rd Qu. 1844.00<br>Max: 3180.00<br>Average: 1844.26 [ 6.50820e-2] ( 1778.54 ‥ 1967.80)<br>Std. Dev: 320.492 [ -13.6930] ( 177.997 ‥ 502.461)<br><br>Outliers: 3/6 = 9 (μ=1844.33, σ=306.799)<br> Outlier variance: 0.852347 (severe, the data set is probably unusable)<br><br>------<br><br>Dataset: + N=50 CI=95.0000<br>Statistic Value [ Bias] (Bootstrapped LB‥UB)<br>Min: 2339.00<br>1st Qu. 2779.00<br>Median: 2965.00<br>3rd Qu. 3017.00<br>Max: 3523.00<br>Average: 2903.64 [ -1.28160e-2] ( 2847.28 ‥ 2959.10)<br>Std. Dev: 200.957 [ -3.90939] ( 159.507 ‥ 269.636)<br><br>Outliers: 1/1 = 2 (μ=2903.63, σ=197.048)<br> Outlier variance: 0.483689 (moderate)<br><br>Difference at 95.0% confidence<br> 1059.38 ± 106.139<br> 57.4420% ± 5.75510%<br> (Student's t, pooled s = 267.487)<br>------<br><br>Dataset: * N=50 CI=95.0000<br>Statistic Value [ Bias] (Bootstrapped LB‥UB)<br>Min: 1841.00<br>1st Qu. 1927.00<br>Median: 2158.00<br>3rd Qu. 2300.00<br>Max: 2586.00<br>Average: 2125.90 [ -0.186946] ( 2070.88 ‥ 2184.36)<br>Std. Dev: 206.241 [ -2.58143] ( 189.011 ‥ 239.516)<br><br>Outliers: 0/0 = 0 (μ=2125.71, σ=203.660)<br> Outlier variance: 0.645536 (severe, the data set is probably unusable)<br><br>Difference at 95.0% confidence<br> 281.640 ± 106.934<br> 15.2712% ± 5.79820%<br> (Student's t, pooled s = 269.491)<br>------<br><br>ok<br>19><br></div><div class="gmail_extra"><br clear="all"><br>-- <br><div class="gmail_signature">J.</div>
</div></div>