Re: A slow summation
"Bill Page" <[email protected]>
| Newsgroups | gmane.comp.mathematics.axiom.user |
|---|---|
| Message-ID | <[email protected]> |
On 6/13/07, Alasdair McAndrew wrote:
> Wondering about nice little numeric exercises to help introduce
> Axiom to a small group of students, I attempted to sum the first
> 20000 reciprocals:
>
> reduce(+,[1.0/i for i in 1..20000])
>
> This works, but is (I think) unreasonably slow; it takes over 21
> seconds on my computer. The equivalent command in Maxima
> takes less than 1 second. Is there any way of encouraging Axiom
> to be faster here?
>
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.
(1) -> )set message time on
(1) -> rlist:=new(20000,0.0)$PrimitiveArray(Float);
Type: PrimitiveArray Float
Time: 0 sec
(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.)
Regards,
Bill Page.