<div dir="ltr">Thank you for the feedback. That's very helpful.<div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 7, 2014 at 9:44 AM, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Based on the code at<br>
<br>
<a href="https://github.com/erlang/otp/blob/maint/lib/stdlib/src/binary.erl#L268" target="_blank">https://github.com/erlang/otp/<u></u>blob/maint/lib/stdlib/src/<u></u>binary.erl#L268</a><br>
<br>
It does a lot of splitting, and then a lot more splitting, and then call iolist_to_binary. It looks very inefficient.<br>
<br>
Your solution is the fastest way to do it. You also benefit from match context optimization and so your code is very fast. The only thing that could make it faster is if memory was allocated only once for the resulting binary (instead of realloc a few times)... but maybe there's already an optimization like this?<span class=""><br>
<br>
On 11/07/2014 07:33 PM, Stu Bailey wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
FYI, if you want to try to replicate it, I was processing ~80 chunks of<br>
binary where each chunk was about ~250,000,000 bytes. I think you'll<br>
see the difference on just one chunk. I happen to running on a 8-core<br>
MacBook Pro with 16GB Ram and therefore spawned a process per chunk to<br>
grab all the resources on all the cores. With the hand written<br>
function, it worked like a charm...yay Erlang! :-) I love seeing a few<br>
lines of code effectively use all processing power available. Heats the<br>
machine up quite a bit too. :-)<br>
<br>
On Fri, Nov 7, 2014 at 9:22 AM, Stu Bailey <<a href="mailto:stu.bailey@gmail.com" target="_blank">stu.bailey@gmail.com</a><br></span><span class="">
<mailto:<a href="mailto:stu.bailey@gmail.com" target="_blank">stu.bailey@gmail.com</a>>> wrote:<br>
<br>
I'm not planning to spend a lot of time on this right now, but the<br>
binary:replace(...) was chewing a tremendous amount of system time<br>
CPU load (and actually never finished before I got frustrated and<br>
killed it) and my function was reporting the CPU load as 99% user<br>
time (not system time) and finished in a reasonable time. I assume<br>
the high system time usage for binary:replace(..) is because<br>
binary:replace(...) is doing something manic with system calls for<br>
memory management or something?<br>
<br>
<br>
On Fri, Nov 7, 2014 at 1:44 AM, Loïc Hoguin <<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a><br></span><span class="">
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>> wrote:<br>
<br>
binary:split and binary:replace, unlike other functions of the<br>
binary module, are normal Erlang functions. They also process a<br>
list of options before doing the actual work, so there's an<br>
obvious overhead compared to not doing that. In addition as has<br>
been pointed out, your code is more specialized so that helps too.<br>
<br>
On 11/07/2014 03:33 AM, Stu Bailey wrote:<br>
<br>
I found<br>
<br></span>
binary:replace(BinChunk,<<"\n"<u></u>__>>,<<>>,[global]).<br>
<br>
/significantly /slower than<br>
<br>
remove_pattern(BinChunk,<<>>,<<u></u>__<"\n">>).<br>
<br>
with<br>
<br>
remove_pattern(<<>>,Acc,___<u></u>BinPat) -><br>
Acc;<br>
remove_pattern(Bin,Acc,BinPat)<u></u>__-><span class=""><br>
<<Byte:1/binary,Rest/binary>> = Bin,<br>
case Byte == BinPat of<br></span>
true -> remove_pattern(Rest,Acc,__<u></u>BinPat);<br>
false -><br>
remove_pattern(Rest,<<Acc/__<u></u>binary,Byte/binary>>,BinPat)<span class=""><br>
end.<br>
<br>
That was surprising to me. The built-in binary:replace()<br>
was much much<br>
slower for larger BinChunk with lots of <<"\n">> sprinkled<br>
through.<br>
<br>
Thoughts?<br>
<br>
<br></span>
______________________________<u></u>___________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>><br>
<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a><span class=""><br>
<<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a>><br>
<br>
<br>
--<br>
Loïc Hoguin<br>
<a href="http://ninenines.eu" target="_blank">http://ninenines.eu</a><br>
<br>
<br>
<br>
</span></blockquote><div class="HOEnZb"><div class="h5">
<br>
-- <br>
Loïc Hoguin<br>
<a href="http://ninenines.eu" target="_blank">http://ninenines.eu</a><br>
</div></div></blockquote></div><br></div></div>