[erlang-questions] unpacking big/little endian speed difference

Tony Rogvall <>
Tue Oct 13 16:21:01 CEST 2015


Checking the code with a simple disassembler, that can inspect the loaded code,
using erts_debug:disassemble:

big-endian:

> dis:func(decode_bench, '-loop/2-fun-0-', 2).
000000001813E9A8: [i_func_info_IaaI,0,decode_bench,'-loop/2-fun-0-',2]
000000001813E9D0: [i_bs_start_match2_rfIId,{x,0},{f,403958184},2,0,{x,0}]
000000001813E9F8: [i_bs_get_integer_32_rfId,{x,0},{f,403958480},2,{x,2}]
000000001813EA18: [i_bs_skip_bits_all2_frI,{f,403958480},{x,0},8]
000000001813EA30: [i_bs_start_match2_xfIId,{x,1},{f,403958480},3,0,{x,3}]
000000001813EA60: [i_bs_get_integer_32_xfId,{x,3},{f,403958480},4,{x,4}]
000000001813EA88: [bs_test_unit8_fx,{f,403958480},{x,3}]
000000001813EAA0: [i_fetch_xx,{x,2},{x,4}]
000000001813EAB0: [i_bif2_body_bd,'=<','/',2,{x,0}]
000000001813EAC8: [return]
000000001813EAD0: [bs_context_to_binary_r,{x,0}]
000000001813EAD8: [i_is_lt_spec_frr,{f,403958184},{x,0},{x,0}]

The same for little-endian:

> dis:func(decode_bench, '-loop/2-fun-0-', 2).
0000000018121D30: [i_func_info_IaaI,0,decode_bench,'-loop/2-fun-0-',2]
0000000018121D58: [i_bs_start_match2_rfIId,{x,0},{f,403840304},2,0,{x,0}]
0000000018121D80: [i_bs_get_integer_small_imm_rIfId,
                   {x,0},
                   32,
                   {f,403840616},
                   2,
                   {x,2}]
0000000018121DA8: [i_bs_skip_bits_all2_frI,{f,403840616},{x,0},8]
0000000018121DC0: [i_bs_start_match2_xfIId,{x,1},{f,403840616},3,0,{x,3}]
0000000018121DF0: [i_bs_get_integer_small_imm_xIfId,
                   {x,3},
                   32,
                   {f,403840616},
                   2,
                   {x,4}]
0000000018121E20: [bs_test_unit8_fx,{f,403840616},{x,3}]
0000000018121E38: [i_fetch_xx,{x,2},{x,4}]
0000000018121E48: [i_bif2_body_bd,'=<','/',2,{x,0}]
0000000018121E60: [return]
0000000018121E68: [bs_context_to_binary_r,{x,0}]
0000000018121E70: [i_is_lt_spec_frr,{f,403840304},{x,0},{x,0}]

The difference between
i_bs_get_integer_32_rfId
and
i_bs_get_integer_small_imm_xIfId

this is most probably the cause of the performance difference.

/Tony



> On 13 okt 2015, at 14:59, Sergej Jurečko <> wrote:
> 
> How come unpacking integers as big endian is faster than little endian, when I'm running on a little endian platform (intel osx)?
> I know big endian is erlang default, but if you're turning binaries into integers, you must turn them into little endian anyway, so unpacking should basically be memmcpy.
> 
> The test:
> 
> % Takes:  ~1.3s
> -define(ENDIAN,unsigned-big).
> 
> % Takes: ~1.8s
> % -define(ENDIAN,unsigned-little).
> 
> loop() ->
>     L = [<<(random:uniform(1000000000)):32/?ENDIAN,1:32>> || _ <- lists:seq(1,1000)],
>     S = os:timestamp(),
>     loop1(1000,L),
>     timer:now_diff(os:timestamp(),S).
> 
> loop1(0,_) ->
>     ok;
> loop1(N,L) ->
>     lists:sort(fun(<<A:32/?ENDIAN,_/binary>>,<<B:32/?ENDIAN,_/binary>>) -> A =< B end, L),
>     loop1(N-1,L).
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151013/04427840/attachment.bin>


More information about the erlang-questions mailing list