Thank you Paul. It appears to me that they don't compile down to the same code. Am I missing something?<br><br><div class="gmail_quote">On Mon, Aug 27, 2012 at 3:35 PM, Paul Oliver <span dir="ltr"><<a href="mailto:puzza007@gmail.com" target="_blank">puzza007@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You can use erlc +to_core to get the core erlang:<div><br></div><div><div>module 'test' ['is_greater_than'/2,</div>
<div><span style="white-space:pre-wrap"> </span> 'is_greater_than_control'/2,</div>
<div><span style="white-space:pre-wrap"> </span> 'is_true'/1,</div><div><span style="white-space:pre-wrap"> </span> 'module_info'/0,</div><div><span style="white-space:pre-wrap"> </span> 'module_info'/1]</div>
<div> attributes []</div><div>'is_true'/1 =</div><div> %% Line 5</div><div> fun (_cor0) -></div><div><span style="white-space:pre-wrap"> </span>case _cor0 of</div><div><span style="white-space:pre-wrap"> </span> <'true'> when 'true' -></div>
<div><span style="white-space:pre-wrap"> </span> %% Line 6</div><div><span style="white-space:pre-wrap"> </span> 'true'</div><div><span style="white-space:pre-wrap"> </span> %% Line 7</div>
<div><span style="white-space:pre-wrap"> </span> <'false'> when 'true' -></div><div><span style="white-space:pre-wrap"> </span> %% Line 8</div><div><span style="white-space:pre-wrap"> </span> 'false'</div>
<div><span style="white-space:pre-wrap"> </span> ( <_cor1> when 'true' -></div><div><span style="white-space:pre-wrap"> </span>( primop 'match_fail'</div><div>
<span style="white-space:pre-wrap"> </span> ({'function_clause',_cor1})</div><div><span style="white-space:pre-wrap"> </span> -| [{'function_name',{'is_true',1}}] )</div>
<div><span style="white-space:pre-wrap"> </span> -| ['compiler_generated'] )</div><div><span style="white-space:pre-wrap"> </span>end</div><div>'is_greater_than'/2 =</div>
<div> %% Line 10</div><div> fun (_cor1,_cor0) -></div><div><span style="white-space:pre-wrap"> </span>let <_cor2> =</div><div><span style="white-space:pre-wrap"> </span> %% Line 11</div>
<div><span style="white-space:pre-wrap"> </span> call 'erlang':'>'</div><div><span style="white-space:pre-wrap"> </span>(_cor1, _cor0)</div><div><span style="white-space:pre-wrap"> </span>in %% Line 11</div>
<div><span style="white-space:pre-wrap"> </span> apply 'is_true'/1</div><div><span style="white-space:pre-wrap"> </span>(_cor2)</div><div>'is_greater_than_control'/2 =</div>
<div> %% Line 13</div><div> fun (_cor1,_cor0) -></div><div><span style="white-space:pre-wrap"> </span>%% Line 15</div><div><span style="white-space:pre-wrap"> </span>case <> of</div>
<div><span style="white-space:pre-wrap"> </span> %% Line 16</div><div><span style="white-space:pre-wrap"> </span> <></div><div><span style="white-space:pre-wrap"> </span> when call 'erlang':'>'</div>
<div><span style="white-space:pre-wrap"> </span> (_cor1,</div><div><span style="white-space:pre-wrap"> </span> _cor0) -></div><div><span style="white-space:pre-wrap"> </span> %% Line 17</div>
<div><span style="white-space:pre-wrap"> </span> 'true'</div><div><span style="white-space:pre-wrap"> </span> %% Line 18</div><div><span style="white-space:pre-wrap"> </span> <> when 'true' -></div>
<div><span style="white-space:pre-wrap"> </span> %% Line 19</div><div><span style="white-space:pre-wrap"> </span> 'false'</div><div><span style="white-space:pre-wrap"> </span>end</div>
<div>'module_info'/0 =</div><div> fun () -></div><div><span style="white-space:pre-wrap"> </span>call 'erlang':'get_module_info'</div><div><span style="white-space:pre-wrap"> </span> ('test')</div>
<div>'module_info'/1 =</div><div> fun (_cor0) -></div><div><span style="white-space:pre-wrap"> </span>call 'erlang':'get_module_info'</div><div><span style="white-space:pre-wrap"> </span> ('test', _cor0)</div>
<div>end</div><div><div class="h5"><br><div class="gmail_quote">On Mon, Aug 27, 2012 at 5:44 PM, Jayson Barley <span dir="ltr"><<a href="mailto:jayson.barley@gmail.com" target="_blank">jayson.barley@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
That is interesting. I didn't know that. I wonder why over 100 million iterations I consistently get between 1 to 2 second differences in timing. I know that there will be differences due to garbage collection, other processes running, etc, but the is_greater_than_control/2 function always outperforms the is_greater_than/2 function. Am I making a wrong assumption and eventually the opposite would be true? Could you test the below code on your system and let me know if you get similar results? <br>
<br>-module(test).<br>-compile(export_all).<div><br><br>is_true(true) -><br> true;<br>is_true(false) -><br> false.<br><br>is_greater_than(X, Y) -><br> is_true(X>Y).<br><br></div>is_greater_than_control(X, Y) -><div>
<br>
if<br> X>Y -><br> true;<br> true -> % works as an 'else' branch<br> false<br> end.<br><br></div>test_if(X, X, _) -><br> done;<br>test_if(X, Max, Fun) -><br>
Fun(X, Max),<br>
test_if(X + 1, Max, Fun).<br><br>test_if(Max) -><br> [timer:tc(test, test_if, [0, Max, fun is_greater_than/2]), timer:tc(test, test_if, [0, Max, fun is_greater_than_control/2])].<br><br>8> test:test_if(100000000).<br>
[{7234000,done},{5718999,done}]<div><div><br><br><div class="gmail_quote">On Mon, Aug 27, 2012 at 2:00 PM, Andrzej Sliwa <span dir="ltr"><<a href="mailto:andrzej.sliwa@i-tool.eu" target="_blank">andrzej.sliwa@i-tool.eu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">there is no difference in speed at all,<br>
<div><br>
> switch_signal({signal, _What, _From, _To}) -><br>
> true;<br>
> switch_signal({signal, _What, _To}) -><br>
> true;<br>
> switch_signal(_Else) -><br>
> false.<br>
<br>
</div>is compiled internally to one function with case<br>
so code speed is equal<br>
<br>
there is only difference in readability.<br>
<div><div><br>
On Aug 27, 2012, at 10:56 PM, Jayson Barley <<a href="mailto:jayson.barley@gmail.com" target="_blank">jayson.barley@gmail.com</a>> wrote:<br>
<br>
> I am not sure I understand why we have them. For instance I can take the following code<br>
> is_greater_than(X, Y) -><br>
> if<br>
> X>Y -><br>
> true;<br>
> true -> % works as an 'else' branch<br>
> false<br>
> end.<br>
> And make it<br>
> is_true(true) -><br>
> true;<br>
> is_true(false) -><br>
> false.<br>
><br>
> is_greater_than(X, Y) -><br>
> is_true(X>Y).<br>
> I can do the same thing with case statements<br>
> is_valid_signal(Signal) -><br>
> case Signal of<br>
> {signal, _What, _From, _To} -><br>
> true;<br>
> {signal, _What, _To} -><br>
> true;<br>
> _Else -><br>
> false<br>
> end.<br>
> Becomes<br>
> switch_signal({signal, _What, _From, _To}) -><br>
> true;<br>
> switch_signal({signal, _What, _To}) -><br>
> true;<br>
> switch_signal(_Else) -><br>
> false.<br>
><br>
> is_valid_signal(Signal) -><br>
> switch_signal(Signal).<br>
><br>
> I know that the control structures are a little bit faster, not much, but I find that the function form is more readable.<br>
</div></div><div><div>> _______________________________________________<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" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
</div></div></blockquote></div><br>
</div></div><br>_______________________________________________<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" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br>