Skip to content

Instantly share code, notes, and snippets.

@arcusfelis
Created December 3, 2014 21:49
Show Gist options
  • Save arcusfelis/7ab73572d6bb8bdeea59 to your computer and use it in GitHub Desktop.
Save arcusfelis/7ab73572d6bb8bdeea59 to your computer and use it in GitHub Desktop.
erlang binary performance
-module(sad).
-export([sad1/3,
sad2/3]).
sad1(<<H1,T1/binary>>, <<H2,T2/binary>>, Sum) ->
sad1(T1, T2, Sum + abs(H1-H2));
sad1(<<>>, <<>>, Sum) ->
Sum.
sad2(<<H1,A1,B1,C1,T1/binary>>, <<H2,A2,B2,C2,T2/binary>>, Sum) ->
sad2(T1, T2, Sum + abs(H1-H2) + abs(A1-A2) + abs(B1-B2) + abs(C1-C2));
sad2(T1, T2, Sum) ->
sad1(T1, T2, Sum).
B1 = list_to_binary(lists:seq(1, 255)).
B2 = list_to_binary(lists:seq(255, 1, -1)).
11> timer:tc(sad, sad1, [B1,B2,0]).
{50,32512}
12> timer:tc(sad, sad2, [B1,B2,0]).
{34,32512}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment