From: Didier Verna
Subject: [Q] Optimizing dotimes
Date: 
Message-ID: <mux64sn4ukf.fsf@uzeb.lrde.epita.fr>
Consider the following file to be compiled:

 ,----
| (eval-when (:compile-toplevel)
|   (declaim (optimize (speed 3)
|                      (compilation-speed 0)
|                      (safety 0)
|                      (debug 0))))
| 
| (defun test (my-vector)
|   (declare (type (simple-array fixnum) my-vector))
|   (dotimes (i (car (array-dimensions my-vector)))
|     (setf (aref my-vector i) (+ (aref my-vector i) 3))))
`-----

1/ Compiling with CMU CL gives me this note:

; In: DEFUN TEST

;   (DOTIMES (I #) (SETF # #))
; --> DO BLOCK LET TAGBODY UNLESS COND IF NOT IF >= IF
; ==>
;   (< I #:G0)
; Note: Forced to do GENERIC-< (cost 10).
;     Unable to do inline fixnum comparison (cost 4) because:
;     The second argument is a REAL, not a FIXNUM.


If I *know* that my vector size can be represented by a fixnum, I have found
that I can say:

(dotimes (i (the fixnum (car (array-dimensions my-vector)))) ...)

helps supressing the note (and optimizing further I hope ;-)



2/ There are also times where I get a note like the following (I haven't been
   able to figure out exactly in what circumstances yet):

; --> DO BLOCK LET TAGBODY PSETQ PSETF LET* MULTIPLE-VALUE-BIND LET 1+
; ==>
;   (+ I 1)
; Note: Forced to do GENERIC-+ (cost 10).
;     Unable to do inline fixnum arithmetic (cost 1) because:
;     The first argument is a UNSIGNED-BYTE, not a FIXNUM.
;     The result is a (INTEGER 1), not a FIXNUM.
;     Unable to do inline fixnum arithmetic (cost 2) because:
;     The first argument is a UNSIGNED-BYTE, not a FIXNUM.
;     The result is a (INTEGER 1), not a FIXNUM.
;     etc.


There, issuing a (declare (type fixnum i)) right after the first dotimes
argument helps.


So everything's fine, but I'm a bit suspicious about my trickery. So I'd like
to know if I'm doing the right thing...


Thanks !

-- 
Didier Verna, ······@lrde.epita.fr, http://www.lrde.epita.fr/~didier

EPITA / LRDE, 14-16 rue Voltaire   Tel.+33 (1) 44 08 01 85
94276 Le Kremlin-Bic�tre, France   Fax.+33 (1) 53 14 59 22   ······@xemacs.org

From: tichy
Subject: Re: Optimizing dotimes
Date: 
Message-ID: <1127769220.864137.221810@z14g2000cwz.googlegroups.com>
Hi.
> (dotimes (i (car (array-dimensions my-vector))) ... )

Try this: (dotimes (i (array-dimension my-vector 0)) ...)

Regards.
From: Bulent Murtezaoglu
Subject: Re: [Q] Optimizing dotimes
Date: 
Message-ID: <87hdc7g2og.fsf@p4.internal>
>>>>> "DV" == Didier Verna <······@lrde.epita.fr> writes:
[...]
    DV> If I *know* that my vector size can be represented by a
    DV> fixnum, [...]

This response is unrelated to your real question but I think you 
_always_ know this.  No?

http://www.lispworks.com/documentation/HyperSpec/Body/v_ar_dim.htm

cheers,

BM