Thanks for the improvement. I don't think you need to publish changes like
this to the whole mailing list. This is an optimization which doesn't
change the black-box behavior of the function.
But since you *have* published it, I'll comment on it...: It's probably not
worth your while to make nano-optimizations like this. Though having
'first' be fast on long lists (which it already is) is probably somewhat
useful, saving 5mS on second(<long list>) is unlikely to matter to any real
program.
-s
On Thu, Oct 3, 2013 at 5:19 AM, John Lapeyre <lapeyre.math122a at gmail.com>wrote:
> Greetings,
>
> second(), third(), etc. traverse the entire expression. But, they
> don't need to. Here is a change that fixes that.
>
> Example:
>
> a:create_list(i,i,1,10^6)$
>
> before change:
>
> for i:1 thru 100 do second(a);
> Evaluation took 0.4900 seconds (0.4860 elapsed)
>
> after change:
>
> for i:1 thru 100 do second(a);
> Evaluation took 0.0000 seconds (0.0000 elapsed)
>
> If it is decided that it should be applied, I can
> make the patch. But someone else can do it, too.
> Note especially that
> (make-nth second 2)
> was changed to
> (make-nth second 1), etc.
>
> --------
>
> (macrolet ((make-nth (si i)
> (let ((sim (intern (concatenate 'string "$" (symbol-name si)))))
> `(defmfun ,sim (e)
> (atomchk (setq e (format1 e)) ',sim nil)
> (handler-case
> (elt (margs e) ,i)
> (error ()
> (merror (intl:gettext "~:M: no such element in
> ~M") ',sim e)))))))
> ; (if (< (length (margs e)) ,i)
> ; (merror (intl:gettext "~:M: no such element in ~M") ',sim
> e))
> ; (,si (margs e))))))
>
> (make-nth second 1)
> (make-nth third 2)
> (make-nth fourth 3)
> (make-nth fifth 4)
> (make-nth sixth 5)
> (make-nth seventh 6)
> (make-nth eighth 7)
> (make-nth ninth 8)
> (make-nth tenth 9))
>
> ______________________________**_________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/**mailman/listinfo/maxima<http://www.math.utexas.edu/mailman/listinfo/maxima>
>