<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""></div><div class=""><br class=""></div><br class=""><div class=""><br class=""><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 08 Jun 2021, at 07:45, Jacob <<a href="mailto:jacob01@gmx.net" class="">jacob01@gmx.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi,<br class=""><br class="">I've tried to reproduce the measurement, but according to my<br class="">measurements, there is just a factor of 2 on Erlang/OTP 22.<br class=""><br class="">1> timer:tc(fun () -> bench:t2b(a, 1000000) end)<br class="">{109357,<<131,100,0,1,97>>}<br class="">2> timer:tc(fun () -> bench:b2t(<<131,100,0,1,97>>, 1000000) end).<br class="">{199488,a}<br class=""><br class=""><br class="">If I do not use the result of each term_to_binary call, the factor (~14)<br class="">is much closer to your measurements:<br class=""><br class="">3> timer:tc(fun () -> bench:broken_t2b(a, 1000000) end).<br class="">{14404,<<>>}<br class=""><br class="">Are you indeed sure, that the compiler did not optimise away the entire<br class="">call?<br class=""><br class="">/Jacob<br class=""><br class="">======================== bench.erl ==============================<br class="">-module(bench).<br class=""><br class="">-export([t2b/2, b2t/2, broken_t2b/2]).<br class=""><br class=""><br class="">t2b(T, N) -> t2b(T, N, undefined).<br class=""><br class="">t2b(_, 0, R) -> R;<br class="">t2b(T, N, _) -> R = term_to_binary(T), t2b(T, N-1, R).<br class=""><br class="">b2t(T, N) -> b2t(T, N, undefined).<br class=""><br class="">b2t(_, 0, R) -> R;<br class="">b2t(T, N, _) -> R = binary_to_term(T), b2t(T, N-1, R).<br class=""><br class="">broken_t2b(T, N) -> broken_t2b(T, N, undefined).<br class=""><br class="">broken_t2b(_, 0, R) -> R;<br class="">broken_t2b(T, N, R) -> _ = term_to_binary(T), broken_t2b(T, N-1, R).<br class="">=================================================================<br class=""><br class=""><br class="">On 06.06.21 02:07, Valentin Micic wrote:<br class=""><blockquote type="cite" class="">Hi all,<br class=""><br class="">I did some performance measurement recently that included conversion of<br class="">an arbitrary erlang term to its external binary representation via<br class="">term_to_binary/1, as well as reversing the result using binary_to_term/1.<br class=""><br class="">I’ve noticed that term_to_binary/1 is significantly faster than<br class="">binary_to_term/1.<br class=""><br class="">Also, I’ve observed that binary_to_term/1 performance gets considerably<br class="">worse as complexity of specified term increases, whilst term_to_binary/1<br class="">maintains (more-less) steady performance.<br class=""><br class="">(cig@MacBook-Pro)40> tconvert:run( a, 10000000 ).<br class=""><br class="">term_to_binary/1 RETURN VALUE:<<131,100,0,1,97>><br class="">REQUEST COUNT:10000000<br class="">ELAPSED TIME (usec):97070<br class="">TIME PER REQUEST (usec): 0.009707<br class="">PROJECTED RATE (req/sec): *103018440*.30081384<br class=""><br class="">binary_to_term/1 RETURN VALUE:a<br class="">REQUEST COUNT:10000000<br class="">ELAPSED TIME (usec):3383483<br class="">TIME PER REQUEST (usec): 0.3383483<br class="">PROJECTED RATE (req/sec): *2955534*.2822765773<br class="">ok<br class=""><br class="">(cig@MacBook-Pro)41> tconvert:run( {a,<<1,2,3>>, b, [1,2,3], c, {1,2,3},<br class="">d, #{a=>1, b=>2, c=>3}}, 10000000 ).<br class=""><br class="">term_to_binary/1 RETURN<br class="">VALUE:<<131,104,8,100,0,1,97,109,0,0,0,3,1,2,3,100,0,1,<br class=""> <br class="">98,107,0,3,1,2,3,100,0,1,99,104,3,97,1,97,2,97,<br class=""> <br class="">3,100,0,1,100,116,0,0,0,3,100,0,1,97,97,1,100,<br class=""> 0,1,98,97,2,100,0,1,99,97,3>><br class="">REQUEST COUNT:10000000<br class="">ELAPSED TIME (usec):97307<br class="">TIME PER REQUEST (usec): 0.0097307<br class="">PROJECTED RATE (req/sec): *102767529*.57135664<br class=""><br class="">binary_to_term/1 RETURN VALUE:{a,<<1,2,3>>,<br class=""> b,<br class=""> [1,2,3],<br class=""> c,<br class=""> {1,2,3},<br class=""> d, <br class=""> #{a => 1,b => 2,c => 3}}<br class="">REQUEST COUNT:10000000<br class="">ELAPSED TIME (usec):8747426<br class="">TIME PER REQUEST (usec): 0.8747426<br class="">PROJECTED RATE (req/sec): *1143193*.4377038456<br class="">ok<br class=""><br class=""><br class=""><br class="">I’ve performed testing on R21.1.<br class="">Any thoughts?<br class=""><br class="">V/<br class=""></blockquote><br class=""></div></div></blockquote></div><br class=""></div></div></body></html>