<div dir="ltr">John, did you measure in the shell?<div><br></div><div>Code in the shell is evaluated and it won't give the same result as if the code would be compiled as part of a module.</div><div><br></div><div>Ideally you want to define a module, compile it, and then execute a function that runs the benchmarking.</div><div class="gmail_extra"><div><div class="m_7204029001756331320gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div><br></div><div><span style="font-size:13px"><div><span style="font-family:arial,sans-serif;font-size:13px;border-collapse:collapse"><b>José Valim</b></span></div><div><span style="font-family:arial,sans-serif;font-size:13px;border-collapse:collapse"><div><span style="font-family:verdana,sans-serif;font-size:x-small"><a href="http://www.plataformatec.com.br/" style="color:rgb(42,93,176)" target="_blank">www.plataformatec.com.br</a></span></div><div><span style="font-family:verdana,sans-serif;font-size:x-small">Skype: jv.ptec</span></div><div><span style="font-family:verdana,sans-serif;font-size:x-small">Founder and Director of R&D</span></div></span></div></span></div></div></div></div></div>
<br><div class="gmail_quote">On Sun, Apr 16, 2017 at 1:53 AM, John Doe <span dir="ltr"><<a href="mailto:donpedrothird@gmail.com" target="_blank">donpedrothird@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Just've checked, the first one is approx 2 times faster on 19.1.6<div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">1> L = lists:seq(1, 1000000).
</span><br>2> N = -1.
<br>3> T = {error, -1}.
<br>4> F1 = fun() -> lists:foreach(fun(_) -> case T of {success, Z} -> Z; {error, Z} -> Z end end, L) end.
<br>5> F2 = fun() -> lists:foreach(fun(_) -> case N of X when X > 0 -> X; _ -> N end end, L) end.
<br>6> timer:tc(F1). <br>{1089950,ok}
<br>7> <br>7> timer:tc(F2).
<br>{</span><span style="font-family:monospace"><span style="color:rgb(0,0,0)">1870456</span></span><span style="font-family:monospace">,ok}<br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-04-15 21:13 GMT+03:00 Mikael Pettersson <span dir="ltr"><<a href="mailto:mikpelinux@gmail.com" target="_blank">mikpelinux@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Suppose a frequently called function returns a positive integer (always a fixnum)<br>
and a boolean "success" or "failure" indicator (even in the failure case the return<br>
value is significant and will be used). Which is better from a performance<br>
perspective:<br>
<br>
1. Tag the return value, {success, X} or {failure, Y}, and have callers pattern-match<br>
on that<br>
<br>
or,<br>
<br>
2. Indicate failure by negating Y, and have callers match on<br>
<br>
X when X > 0 -> success case;<br>
MinusY -> failure case % MinusY =< 0 is implicit<br>
<br>
Option 2 avoids the consing overheads of option 1, but I'm worried that the X > 0<br>
guard may be less optimized than a plain structural pattern-match. The last time<br>
I checked, term-comparisons would check inline for identity, and otherwise call<br>
the general cmp() function which would then do a lot of type-casing.<br>
<br>
In my pure Erlang code I'm using option 1, but I'm reimplementing the function<br>
as a NIF, and would like to avoid the consing overheads is that's indeed better.<br>
<br>
<br>
/Mikael<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
</blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
<br></blockquote></div><br></div></div>