[erlang-questions] Deforesting tuple updates

Per Gustafsson per.gustafsson@REDACTED
Thu Feb 22 17:41:01 CET 2007


Joel Reymont wrote:
> I don't think it works for floats or doubles. It's just bytes or  
> fixnums if I remember it correctly.
> 
> On Feb 22, 2007, at 3:32 PM, Daniel Luna wrote:
> 
> 
>>As long as the values in the array are simple terms, you can use  
>>hipe_bifs:bytearray/2.
> 
> 
> --
> http://wagerlabs.com/
> 
> 
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions

You could use this code:

-module(floats).

-export([new/1,update/3,sum/1,
	new2/1,update2/3,sum2/1,
	test/0]).

new(N) ->
   hipe_bifs:bytearray(N*8,0).

update(Arr,N,Float) ->
   <<A1,A2,A3,A4,A5,A6,A7,A8>> = <<Float/float>>,
   Start=N*8,
   hipe_bifs:bytearray_update(Arr,Start,A1),
   hipe_bifs:bytearray_update(Arr,Start+1,A2),
   hipe_bifs:bytearray_update(Arr,Start+2,A3),
   hipe_bifs:bytearray_update(Arr,Start+3,A4),
   hipe_bifs:bytearray_update(Arr,Start+4,A5),
   hipe_bifs:bytearray_update(Arr,Start+5,A6),
   hipe_bifs:bytearray_update(Arr,Start+6,A7),
   hipe_bifs:bytearray_update(Arr,Start+7,A8).

sum(Bin) ->
   sum(Bin,0.0).

sum(<<Float/float,Rest/binary>>, Acc) ->
   sum(Rest,Float+Acc);
sum(<<>>,Acc) -> Acc.

Performance is not that great, about 4-5 times faster updates than 
gb_trees for arrays of 100000 floats, and summing is slower.

Per



More information about the erlang-questions mailing list