Ruud H.G. van Tol on Tue, 12 Mar 2024 10:24:13 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Avoid using "listput()" if possible?




On 2024-03-12 09:06, hermann@stamm-wilbrandt.de wrote:
Yesterday I was happy to have working "SquaresRepresentations(n,k)" using "for(...foreach(...listput(..)))".
[...]
I see that "listput()" is definitely needed in some scenarios. Question is, whether using lists should be avoided if possible as above? Without list above looks nicer to me; is a general statement about runtimes with/without list on big inputs possible?

concat() is fine for vectors of short length.

? my(N=2^12, v=[]); for(i=1,N, v=concat([i],v));
cpu time = 119 ms, real time = 120 ms.

? my(N=2^12, v=List()); for(i=1,N, listput(v,i)); Vecrev(v);
cpu time = 3 ms, real time = 3 ms.

-- Ruud