<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On 04 Jun 2016, at 12:52 PM, Loïc Hoguin wrote:</div><br><blockquote type="cite"><div>On the other hand, if your input is a binary, converting + looping might be slower than just looping through the binary. Not to mention the extra garbage created.<br><br>This section should point this out, otherwise we'll end up with users converting binaries to list "because lists are faster for this".<br><br>Also I think there are too few cases where lists win, especially in that kind of comparison. You are basically comparing binaries-as-string vs strings, and when using those it's frequent to match against <<"abcdef", T/binary>> forms (vs [$a, $b, $c, $d, $e, $f|T]) and as you pointed out, in that case binaries win.<br><br>IMO the myth is that list strings are efficient at all when in fact parsing them or even just having them in memory is inefficient. They have their use cases (Unicode related mostly).<br><br>Of course if the section is about people storing lists of integers in a binary then it makes perfect sense, but I haven't seen that one yet. :-)<br><br>-- <br>Loïc Hoguin<br><a href="http://ninenines.eu">http://ninenines.eu</a><br>Author of The Erlanger Playbook,<br>A book about software development using Erlang<br></div></blockquote><br></div><div>Consider the following functional equivalents <b>(A)</b> and <b>(B)</b>:</div><div><br></div><div><b>(A)</b></div><div><div><font class="Apple-style-span" face="'Courier New'">twist_tripod( <<>>, <<>>, <<>>, <<Result/binary>> ) -> Result;</font></div><div><font class="Apple-style-span" face="'Courier New'">twist_tripod( <<A0:1, A1:1, A2:1, A3:1, A4:1, A5:1, A6:1, A7:1, RemA/binary>>,</font></div><div><font class="Apple-style-span" face="'Courier New'"> <<B0:1, B1:1, B2:1, B3:1, B4:1, B5:1, B6:1, B7:1, RemB/binary>>,</font></div><div><font class="Apple-style-span" face="'Courier New'"> <<C0:1, C1:1, C2:1, C3:1, C4:1, C5:1, C6:1, C7:1, RemC/binary>>,</font></div><div><font class="Apple-style-span" face="'Courier New'"> <<Result/binary>> )</font></div><div><font class="Apple-style-span" face="'Courier New'">-></font></div><div><font class="Apple-style-span" face="'Courier New'"> twist_tripod( RemA, RemB, RemC, <<A0:1, B0:1, C0:1,</font></div><div><font class="Apple-style-span" face="'Courier New'"> A1:1, B1:1, C1:1,</font></div><div><font class="Apple-style-span" face="'Courier New'"> A2:1, B2:1, C2:1,</font></div><div><font class="Apple-style-span" face="'Courier New'"> A3:1, B3:1, C3:1,</font></div><div><font class="Apple-style-span" face="'Courier New'"> A4:1, B4:1, C4:1,</font></div><div><font class="Apple-style-span" face="'Courier New'"> A5:1, B5:1, C5:1,</font></div><div><font class="Apple-style-span" face="'Courier New'"> A6:1, B6:1, C6:1,</font></div><div><font class="Apple-style-span" face="'Courier New'"> A7:1, B7:1, C7:1, Result/binary>> )</font></div><div><font class="Apple-style-span" face="'Courier New'">.</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><b>(B)</b></div><div><div><span class="Apple-style-span" style="font-family: 'Courier New'; ">twist_tripod</span><font class="Apple-style-span" face="'Courier New'">( [], [], [], Result ) -> Result;</font></div><div><span class="Apple-style-span" style="font-family: 'Courier New'; ">twist_tripod</span><font class="Apple-style-span" face="'Courier New'">( [A|LA], [B|LB], [C|LC], Result )</font></div><div><font class="Apple-style-span" face="'Courier New'">-></font></div><div><font class="Apple-style-span" face="'Courier New'"> Out_0 = (A band 16#80) bor % a7</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((B band 16#80) bsr 1) bor % b7</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((C band 16#80) bsr 2) bor % c7</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((A band 16#40) bsr 2) bor % a6</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((B band 16#40) bsr 3) bor % b6</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((C band 16#40) bsr 4) bor % c6</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((A band 16#20) bsr 4) bor % a5</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((B band 16#20) bsr 5), % b5</font></div><div><font class="Apple-style-span" face="'Courier New'"> </font></div><div><font class="Apple-style-span" face="'Courier New'"> Out_1 = ((C band 16#20) bsl 2) bor % c5</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((A band 16#10) bsl 2) bor % a4</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((B band 16#10) bsl 1) bor % b4</font></div><div><font class="Apple-style-span" face="'Courier New'"> (C band 16#10) bor % c4</font></div><div><font class="Apple-style-span" face="'Courier New'"> (A band 16#08) bor % a3</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((B band 16#08) bsr 1) bor % b3</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((C band 16#08) bsr 2) bor % c3</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((A band 16#04) bsr 2), % a2</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'"> Out_2 = ((B band 16#04) bsl 5) bor % b2</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((C band 16#04) bsl 4) bor % c2</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((A band 16#02) bsl 4) bor % a1</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((B band 16#02) bsl 3) bor % b1</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((C band 16#02) bsl 2) bor % c1</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((A band 16#01) bsl 2) bor % a0</font></div><div><font class="Apple-style-span" face="'Courier New'"> ((B band 16#01) bsl 1) bor % b0</font></div><div><font class="Apple-style-span" face="'Courier New'"> (C band 16#01), % c0</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'"> </font><span class="Apple-style-span" style="font-family: 'Courier New'; ">twist_tripod</span><font class="Apple-style-span" face="'Courier New'">( LA, LB, LC, [Out_0, Out_1, Out_2|Result] )</font></div><div><font class="Apple-style-span" face="'Courier New'">.</font></div></div><div><br></div><div>Believe me when I tell you that <b>(B)</b> is considerably faster than <b>(A) -- </b>I know, as I had to write <b>(B)</b> in order to optimize <b>(A)</b>.</div><div>However, if you look at <b>(A)</b>, it seems so much easier to see what is being done. Not so with <b>(B)</b>.</div><div><br></div><div>As it is not always about the speed, maybe the myth is that we use binary matching because it is faster. </div><div>I think we use it because it is so much more expressive, concise and therefore elegant.</div><div><br></div><div>V/</div><div><br></div></div><br></body></html>