From: David Deharbe
Subject: Contest: first results.
Date: 
Message-ID: <CH92DA.DpJ@imag.fr>
-----------------------
Dear Net and LISP users,
-----------------------

I have posted a few days ago a message asking you to optimize some LISP
code. Here are a recall of the problem, and the first results on a 
mid-size benchmark (big sizes take hours). If you are interested in
learning more about format directives, have a look at the code I have
included below, it is very instructive.
I thank a lot all contributors, they all posted versions faster than
mine...
For the moment Eyvind Ness is the winner. And I think it will be difficult
to do better than him, but if your version is faster, I would be happy
to get it (this is a real life problem for me).

Thanks,

David Deharbe.



          # ·············@imag.fr # (+33) 76 63 58 78 #
          #    BP 53 ; F - 38 041 GRENOBLE CEDEX 9    #


P.S. Please, I would prefer answers via e-mail. Thanks in advance.

%%%%%%%%%%%%

The problem:

%%%%%%%%%%%%

I am facing a problem of code optimisation in AKCL (v1.619 Beta). I need a 
function F that takes as input a list of elements that belongs to the set 
{0,1,'X) and format it in a file in a Prolog-like fact. 

For instance:
^^^^^^^^^^^^

  F applyed to '(0 1 X X 1 1) and a stream file-ptr should write to
the file pointed by file-ptr the string "~%[0,1,x,x,1,1].", where
~% stands for a new line, and X's are down cased for Prolog requirements.

In my program this function might be called thousands of time, and the lists
to process may have up to one hundred elements. Maybe should I add that, in
a run of the program, the lists to process will have a known and fixed length.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The different functions and their results.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

;; --------------------------------------------------------------------------
;;
;; real time: 74.233
;; run time: 50.567
;;
(defun print-test-1 (test file-ptr)
  (format file-ptr "~%[")
  (print-logic-1 (car test) file-ptr)
  (print-logic-list-1 (cdr test) file-ptr)
  (format file-ptr "]."))

(defun print-logic-1 (logic file-ptr)
  (cond
   ((numberp logic)
    (format file-ptr "~D" logic))
   (t
    (format file-ptr (string-downcase (string logic))))))

(defun print-logic-list-1 (logics+l file-ptr)
  (cond
   (logics+l
    (format file-ptr ",")
    (print-logic-1 (car logics+l) file-ptr)
    (print-logic-list-1 (cdr logics+l) file-ptr))))

;; --------------------------------------------------------------------------
;;
;; real time: 81.717
;; run time: 70.900
;;
(defun print-test-2 (test file-ptr)
  (do
   ((logics+l test (cdr logics+l))
    (test-str (print-logic-2 (car test))
	      (format nil "~A,~A" test-str (print-logic-2 (car logics+l)))))
   ((null logics+l)
    (format file-ptr "~%[~A]." test-str))))

(defun print-logic-2 (logic)
  (cond
   ((numberp logic)
    (format nil "~D" logic))
   (t
    (string-downcase (string logic)))))

;; --------------------------------------------------------------------------
;; |\/|\/|\/| Per-Erik Martin, Dept. of Computer Systems, Uppsala University,
;; |  |  |/\| Box 325, S-751 05 UPPSALA,  Sweden
;; |/\|  |  | Tel: +46 18183073,  Fax: +46 18550225,   Email: ···@DoCS.UU.SE

;;
;; real time: 47.517
;; run time: 37.417
;;
(defun print-test-3 (x str)
   (flet ((f (e str)                    ; Print one element.
            (if (numberp e)
              (princ e str)
              (princ (string-downcase (string e)) str))))
     ;; Print the leading [
     (terpri str) (princ #\[ str)
     ;; Print the first element.
     (when x
       (f (first x) str))
     ;; Print the rest...
     (dolist (e (rest x))
       (princ #\, str)
       (f e str))
     ;; Print the final ]
     (princ #\] str)
     (princ #\. str)))

(defun print-test-3 (test file-ptr)
  (print-logic-3 test file-ptr))

;; --------------------------------------------------------------------------
;; Eyvind Ness <···········@hrp.no>
;;
;; real time: 38.150
;; run time: 30.167
;;
(defun print-test-4 (test stream)
  (let ((*print-case* :downcase))
    (format stream "~%[~{~A~^,~}]." test)))

;; --------------------------------------------------------------------------
;;> Anthony Berglas
;;> Rm 503, Computer Science, Uni of Qld, 4072, Australia.
;;> Uni Ph +61 7 365 4184,  Home 391 7727,  Fax 365 1999
;;
;; real time: 55.683
;; run time: 46.967
;;
(defun print-test-5 (test stream)
  (format stream "~%[~{~(~S~)~^,~}]." test))


;; --------------------------------------------------------------------------
;;Bernd Loechner (PA Sturm)
··········@informatik.uni-kl.de
;;
;; real time: 46.867
;; run time: 34.117
;;
(defun print-test-6 (test file-ptr)
  (let
      ((*print-case* :downcase))
    (format file-ptr "~%[~s~{,~s~}]." (car test) (cdr test))))






-- 
          # ·············@imag.fr # (+33) 76 63 58 78 #
          #    BP 53 ; F - 38 041 GRENOBLE CEDEX 9    #