Appending to the *end* of a list is expensive. Cons'ing on to the beginning
then reversing is much faster:
(%i1) showtime:all;
Evaluation took 0.0000 seconds (0.0000 elapsed)
(%o1) all
(%i2) (l:[],for i thru 20000 do l:cons(i,l),reverse(l))$
Evaluation took 0.1000 seconds (0.1000 elapsed)
(%i3) (l:[],for i thru 20000 do l:append(l,[i]))$
Evaluation took 4.6300 seconds (4.6300 elapsed)
On Mon, Jun 14, 2010 at 15:55, reyssat <eric.reyssat at math.unicaen.fr> wrote:
> Adam Majewski a ?crit :
>
>> ......reyssat pisze:
>>
>> This is the first method of
>>> http://magma.maths.usyd.edu.au/magma/Examples/node6.html
>>> a maxima implementation is :
>>> fc(n):=block(a:0,b:1,c:1,d:n,
>>> while c<n do
>>> (k:floor ((b+n)/d), e:k*c-a,f:k*d-b, a:c,b:d,c:e,d:f));
>>>
>>
>> Thx but fc gives no result. (:-))
>>
> Yes, this is why I said the elements are computed but not stored.
>
> One way to get a result is to add a print instruction inside the loop :
> (k:floor ((b+n)/d), e:k*c-a,f:k*d-b, a:c,b:d,c:e,d:f, print([a,b])));
> Of course, printing takes time.
>
> An other way is to store the result, maybe by appending the new element :
>
> fc(n):=block(L:[],a:0,b:1,c:1,d:n,
> while c<n do
> (k:floor ((b+n)/d), e:k*c-a,f:k*d-b, a:c,b:d,c:e,d:f,
> L:append(L,[[a,b]])),L);
>
> but I don't know if appending one element each time is efficient or not.
>
> Eric
>
>
>> Adam
>>
>> _______________________________________________
>> Maxima mailing list
>> Maxima at math.utexas.edu
>> http://www.math.utexas.edu/mailman/listinfo/maxima
>>
>>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>