# [erlang-questions] hmac sha256 erlang

Steve Davis <>
Mon May 9 00:19:40 CEST 2011

```I had RTFM before making that suggestion! However, with my interest
mildly piqued by this, I checked a little further, adding a "manual
append" comparison just for good measure:

test(Num) ->
T0 = funtime(fun lcopy/0, Num),
T1 = funtime(fun bcopy/0, Num),
T2 = funtime(fun ccopy/0, Num),
{T0, T1, T2}.
lcopy() ->
list_to_binary(lists:duplicate(128, 16#36)).
bcopy() ->
binary:copy(<<16#36>>, 128).
ccopy() ->
ccopy(<<16#36>>, 128).
ccopy(Bin, Count) when Count > 0 ->
ccopy(<<Bin/binary, 16#36>>, Count - 1);
ccopy(Bin, 0) ->
Bin.

funtime(F, Repeats) ->
{A1, B1, C1} = erlang:now(),
exec_fun(F, Repeats),
{A2, B2, C2} = erlang:now(),
((A2-A1) * 1000000 + (B2-B1)) * 1000 + (C2-C1) / 1000.

exec_fun(_, 0) ->
ok;
exec_fun(F, Count) ->
F(),
exec_fun(F, Count - 1).

1> copier:test(10000).
{265.0,62.999,280.999}

Not really any kind of formal test but it does seem to confirm the

/s

On 5/8/2011 4:49 PM, Bob Ippolito wrote:
> binary:copy/2 is more useful for this purpose.
>
> On Sunday, May 8, 2011, Robert Virding
> <>  wrote:
>> In the docs for binary:copy it says:
>>
>> "This function will always create a new binary, even if N = 1. By
using copy/1 on a binary referencing a larger binary, one might free up
the larger binary for garbage collection."
>>
>> Although there is a caveat saying that it might be actually useful
in special cases.
>>
>> Robert
>>
>> ----- "Steve Davis"<>  wrote:
>>
>>> Maybe not but in this case it seems marginally faster and I can't see
>>> any other impact?
>>>
>>> I'm definitely open to correction/enlightenment, but I don't see why
>>> not here!
>>>
>>> /s
>>>
>>> On May 6, 4:43 pm, Robert Virding<
>>> solutions.com>  wrote:
>>>> Why would you want to do binary:copy here? It is not what it is
>>> intended for.
>>>>
>>>> Robert
>>>>
>>>> ----- "Steve Davis"<>  wrote:
>>>>
>>>>> ...though now I would favor binary:copy instead of
>>>>> list_to_binary(lists:duplicate()), but that depends on what OTP
>>>>> version you have...
>>>>
>>>>> /s
>>
>

```