I must be stupid

Håkan Stenholm hakan.stenholm@REDACTED
Sat Jul 9 21:11:18 CEST 2005


Joel Reymont wrote:

> ....
>
> creates just a second without HiPE whereas doing it like this takes I  
> don't know how much time (stopped after half an hour or so)
>
> populate(T, 0) ->
>     T;
>
> populate(T, Size) ->
>     populate(setelement(Size, T, Size), Size - 1).
>
> Is there a simple explanation?

Yes.

Updating a tuple _allways_ creates a new copy of the updated tuple [1] - 
as there is no destructive update.

Attaching a new element to the head of a list, only allocates memory for 
that element.

In other words a size N tuple in populate/2, will allocate memory for 
N*N elements [2], rather than N elements, as in the populate1/2 call, so 
things will obviously go much slower.

[1]: a new copy of the internal pointer array used - the data pointed to 
will remain unchanged, only one or more tuple pointers will differ in 
the new tuple.
[2]: where N*(N-1) elements of the N*N elements, will later be discarded 
by the garbage collector.

>
>     Thanks, Joel
>
> -- 
> http://wagerlabs.com/uptick
>
>
>
>




More information about the erlang-questions mailing list