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

Do type declarations *always* make things better ?



Consider the following piece of code:

(proclaim '(special *a* *b*))

(defun f1 (x)
	(declare (type fixnum x))
	(the fixnum
		(do* ((i x (the fixnum (1+ i))))
		     ((= i 100) (setq *a* (cons i *a*)) (cons i x))
		     (declare (type fixnum i))
		     (setq *a* (cons i *a*))
		     (setq *b* (cons i *b*)))))

(defun f2 (x)
	(declare (type fixnum x))
	(the fixnum
		(do* ((i x (the fixnum (1+ (the fixnum i)))))
		     ((= (the fixnum i) 100) (setq *a* (cons i *a*)) (cons i x))
		     (setq *a* (cons i *a*))
		     (setq *b* (cons i *b*)))))

f1 involves two CMPmake_fixnum operations in each loop at the two setqs and
two CMPmake_fixnum operations at the exit point for the two conses but at the
same time it does the arithmetic (which here is only 1+ but surely could be
complicated) in fixnum arithmetic.
		      
f2 involves only one CMPmake_fixnum operation at each loop (viz. at the
assignment point) but also two fix operations (one at the incrementation point
and the other at the termination check) in each loop. Nevertheless, it also
does the fixnum arithmetic like in f1.

I now have the following two questions:

[1] Does f2 use less fixnum cells than f1? So if I run f1 and f2 both (say)
one million times each then will f1 cause the garbage collection of fixnum
cells twice as often as what f2 will cause?

[2] Which one of the two is faster (apart from the time taken to garbage
collect)?

I would appreciate if you could give me reasons too. Also keep in mind that
in f2 "i" was never declared to be of type fixnum.

Thanks in advance.

	- Tushar

-------------------------------------------------------------------------------
email : saxena@cs.albany.edu                        Computer Science Department
Phone : 518-442-3388				    SUNY Albany
Tushar Saxena					    Albany NY 12222 (USA)
-------------------------------------------------------------------------------