Re: A slow summation
"Bill Page" <[email protected]>
| Newsgroups | gmane.comp.mathematics.axiom.user |
|---|---|
| Message-ID | <[email protected]> |
On 6/13/07, Bill Page <[email protected]> wrote: > > It turns out that Axiom's list data type is very slow and almost > all the time is spent building the list. You can avoid this by > preallocating an arrary of sufficient size. E.g. > > > (2) -> for i in 1..20000 repeat rlist(i-1):=1.0/i > > Type: Void > Time: 1.27 (EV) + 0.42 (GC) = 1.68 sec > > (3) -> reduce(+,rlist) > > (3) 10.4807282172 29327571 > > Type: Float > Time: 0.10 (EV) = 0.10 sec > > (Times shown are from a 500 MHz P3 running Windows XP > and axiom-0.1.4.) > Here is another probably less obvious solution: (1) -> reduce(+,map(x+->1.0/x,expand(1..20000))) (1) 10.4807282172 29327571 Type: Float Time: 0.73 (EV) + 0.02 (OT) + 0.12 (GC) = 0.87 sec The reason that this is very fast is that 'expand' creates the list "in situ" and the loop is internal to the 'map' operator. See the discussion about 'expand' in http://wiki.axiom-developer.org/SandBoxFloatSegment Regards, Bill Page.