Skip to content

Instantly share code, notes, and snippets.

@sile
Created May 19, 2015 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sile/c8df7cc9a106c7d86039 to your computer and use it in GitHub Desktop.
Save sile/c8df7cc9a106c7d86039 to your computer and use it in GitHub Desktop.
> timer:tc(fun() -> list_to_binary([ io_lib:format("~2.16.0B", [X]) || <<X:8>> <= list_to_binary(lists:seq(0,255)) ]) end).
{1565,
<<"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435"...>>}
> timer:tc(fun() -> << <<if X < 10 -> X + $0; true -> X - 10 + $A end>> || <<X:4>> <= list_to_binary(lists:seq(0,255)) >> end).
{2784,
<<"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435"...>>}
> timer:tc(fun() -> hexlify:hex1(list_to_binary(lists:seq(0,255))) end).
{220,
<<"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435"...>>}
> timer:tc(fun() -> hexlify:hex2(list_to_binary(lists:seq(0,255))) end).
{36,
<<"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435"...>>}
-module(hexlify).
-export([hex1/1, hex2/1]).
hex1(Bin) ->
list_to_binary([ io_lib:format("~2.16.0B", [X]) || <<X:8>> <= Bin ]).
hex2(Bin) ->
<< <<if X < 10 -> X + $0; true -> X - 10 + $A end>> || <<X:4>> <= Bin >>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment