<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="">Yes, your (as well as everyone else’s) explanation may account for binary_to_term/1 being slower, I get it — it’s a story about complexity and all… however, something does not add up.<div class=""><br class=""></div><div class=""><div class="">Consider the first example in my measurements:</div><div class=""><br class=""></div><div class="">The term being converted to external representation is an atom ‘<b class="">a</b>’. </div><div class=""><br class=""></div><div class="">So, conversion from an atom ‘a’ to its external representation: <span style="font-family: Menlo; font-size: 11px; background-color: rgba(227, 227, 227, 0.89);" class=""><<131,100,0,1,97>></span> (using term_to_binary/1) is projected to run at the rate of more than 103 million conversions per second.</div><div class=""><br class=""></div><div class="">Since this term has a very simple external representation: <<131, 100,0, 1, 97>>; one would expect conversion back to its internal representation to be just marginally slower. </div><div class=""><br class=""></div><div class="">And yet, binary_to_term/1 is an order of magnitude (or more than 34 times) slower!</div><div class=""><br class=""></div><div class="">I don’t think that story of complexity can pass a red-face test here. </div><div class=""><br class=""></div><div class="">Kind regards</div><div class=""><br class=""></div><div class="">V/</div><div class=""><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 07 Jun 2021, at 20:22, Antoine Koener <<a href="mailto:antoine.koener@gmail.com" class="">antoine.koener@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div dir="ltr" class=""><br class=""></div><div dir="ltr" class="">Not sure that this will explain all, but reconstruction of terms needs more work than serialising, think about atoms than may need to be created, or complex structure that need allocation. </div><div dir="ltr" class=""><br class=""></div><div dir="ltr" class="">Converting everything to binary don’t need any check at all, whereas the reverse is not true.</div><div dir="ltr" class=""><br class=""></div><div dir="ltr" class="">What do you think?</div><div dir="ltr" class=""><br class=""></div><div dir="ltr" class=""><br class=""></div><div dir="ltr" class=""><br class=""><blockquote type="cite" class="">On 7 Jun 2021, at 14:25, Valentin Micic <<a href="mailto:v@micic.co.za" class="">v@micic.co.za</a>> wrote:<br class=""><br class=""></blockquote></div><blockquote type="cite" class=""><div dir="ltr" class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">Hmmm… I realised that my previous email may be seen as a comment rather than a question, so let me ask the question clearly.<div class=""><br class=""></div><div class="">Given that binary_to_term/1 is about two orders of magnitude slower than term_to_binary/1, is there anyone out there that may have a reasonable explanation for that?</div><div class=""><br class=""></div><div class="">Kind regards</div><div class=""><br class=""></div><div class="">V/</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On 06 Jun 2021, at 02:07, Valentin Micic <<a href="mailto:v@micic.co.za" class="">v@micic.co.za</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">I did some performance measurement recently that included conversion of an arbitrary erlang term to its external binary representation via term_to_binary/1, as well as reversing the result using binary_to_term/1.</div><div class=""><br class=""></div><div class="">I’ve noticed that term_to_binary/1 is significantly faster than binary_to_term/1.</div><div class=""><br class=""></div><div class="">Also, I’ve observed that binary_to_term/1 performance gets considerably worse as complexity of specified term increases, whilst term_to_binary/1 maintains (more-less) steady performance.</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgba(227, 227, 227, 0.89);"><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">(cig@MacBook-Pro)40> tconvert:run( a, 10000000 ).</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">term_to_binary/1 RETURN VALUE:<<131,100,0,1,97>></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">REQUEST COUNT:10000000</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">ELAPSED TIME (usec):97070</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">TIME PER REQUEST (usec): 0.009707</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">PROJECTED RATE (req/sec): <b class="">103018440</b>.30081384</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">binary_to_term/1 RETURN VALUE:a</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">REQUEST COUNT:10000000</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">ELAPSED TIME (usec):3383483</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">TIME PER REQUEST (usec): 0.3383483</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">PROJECTED RATE (req/sec): <b class="">2955534</b>.2822765773</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">ok</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgba(227, 227, 227, 0.89);"><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">(cig@MacBook-Pro)41> tconvert:run( {a,<<1,2,3>>, b, [1,2,3], c, {1,2,3}, d, #{a=>1, b=>2, c=>3}}, 10000000 ).</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">term_to_binary/1 RETURN VALUE:<<131,104,8,100,0,1,97,109,0,0,0,3,1,2,3,100,0,1,</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> 98,107,0,3,1,2,3,100,0,1,99,104,3,97,1,97,2,97,</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> 3,100,0,1,100,116,0,0,0,3,100,0,1,97,97,1,100,</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> 0,1,98,97,2,100,0,1,99,97,3>></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">REQUEST COUNT:10000000</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">ELAPSED TIME (usec):97307</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">TIME PER REQUEST (usec): 0.0097307</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">PROJECTED RATE (req/sec): <b class="">102767529</b>.57135664</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">binary_to_term/1 RETURN VALUE:{a,<<1,2,3>>,</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> b,</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> [1,2,3],</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> c,</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> {1,2,3},</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> d, </span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> #{a => 1,b => 2,c => 3}}</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">REQUEST COUNT:10000000</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">ELAPSED TIME (usec):8747426</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">TIME PER REQUEST (usec): 0.8747426</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">PROJECTED RATE (req/sec): <b class="">1143193</b>.4377038456</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">ok</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div></div></div></div><div class=""><br class=""></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">I’ve performed testing on R21.1.</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">Any thoughts?</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">V/</span></div></div></div></blockquote></div><br class=""></div></div></blockquote></div></div></blockquote></div><br class=""></div></div></div></body></html>