From: David Deharbe
Subject: Contest! Optimise the following function :-)
Date:
Message-ID: <CH3Fwv.Ir2@imag.fr>
-----------------------
Dear Net and LISP users,
-----------------------
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 ``downcased'' 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.
But this fixed length changes from one execution to another.
I wrote two versions of this function, called herein print-test.
Here they are:
;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;;
;; First attempt:
;; ^^^^^^^^^^^^^
;;
(defun print-test (test file-ptr)
;~~~~~~~~~~
;;
;; test is a list of 0, 1 and 'X;
;; file-ptr is a stream.
;;
(format file-ptr "~%[")
(print-logic (car test) file-ptr)
(print-logic-list (cdr test) file-ptr)
(format file-ptr "]."))
(defun print-logic (logic file-ptr)
;~~~~~~~~~~~
;;
;; logic is O, 1 or 'X;
;; file-ptr is a stream.
;;
(cond
((numberp logic)
(format file-ptr "~D" logic))
(t
(format file-ptr (string-downcase (string logic))))))
(defun print-logic-list (logics+l file-ptr)
;~~~~~~~~~~~~~~~~
;;
;; logics+l is a list of 0, 1 and 'X;
;; file-ptr is a stream.
;;
(cond
(logics+l
(format file-ptr ",")
(print-logic (car logics+l) file-ptr)
(print-logic-list (cdr logics+l) file-ptr))))
;;
;; End of first attempt.
;; ^^^^^^^^^^^^^^^^^^^^
;;
;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;;
;; Second attempt:
;; ^^^^^^^^^^^^^^
;;
(defun print-test (test file-ptr)
;~~~~~~~~~~
;;
;; test is a list of 0, 1 and 'X;
;; file-ptr is a stream.
;;
(do
((logics+l test (cdr logics+l))
(test-str (print-logic (car test))
(format nil "~A,~A" test-str (print-logic (car logics+l)))))
((null logics+l)
(format file-ptr "~%[~A]." test-str))))
(defun print-logic (logic)
;~~~~~~~~~~~
;;
;; logic is 0, 1 or 'X
;;
(cond
((numberp logic)
(format nil "~D" logic))
(t
(string-downcase (string logic)))))
;;
;; End of second attempt.
;; ^^^^^^^^^^^^^^^^^^^^^
;;
;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The first version appeared to be faster, but I am not satisfied with the
result. I would be very grateful to hear from you comments and suggestions.
I will post the best version you send, and its author(s) will win my
eternal gratitude :-)
I would prefer answers via e-mail: I have problems to catch up the amount
of informations in this newsgroup...
My address is ·······@aguamrina.imag.fr
Thanks for your attention and your future contribution,
David Deharbe.
--
# ·············@imag.fr # (+33) 76 63 58 78 #
# BP 53 ; F - 38 041 GRENOBLE CEDEX 9 #
In article <··········@imag.fr>, ·······@imag.fr (David Deharbe) writes:
|> 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 ``downcased'' 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.
|> But this fixed length changes from one execution to another.
|>
|> I wrote two versions of this function, called herein print-test.
... stuff deleted
I don't know if it's faster, but it's simpler...
(setq *print-case* :downcase)
(defun print-test (test file-ptr)
(format file-ptr "~%[~s~{,~s~}]." (car test) (cdr test)))
so long, Bernd.
From: Anthony Berglas
Subject: Re: Contest! Optimise the following function : -)
Date:
Message-ID: <16532@uqcspe.cs.uq.oz.au>
In <················@informatik.uni-kl.de> ········@informatik.uni-kl.de (Bernd Loechner (PA Sturm)) writes:
>|> 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 ``downcased'' for Prolog requirements.
>I don't know if it's faster, but it's simpler...
>(setq *print-case* :downcase)
NO! Seting global state like this is dangerous --- How will it
affect the rest of the program. At best one should (let ((*Print-Case* ...
In any case, neither the downcase nor the car/cdr in necessary, just write
(format File-Ptr "~%[~{~(~S~)~^,~}]" Test)
Try doing that in one (unintelligable) line of Prolog!
Anthony.
--
Anthony Berglas
Rm 503, Computer Science, Uni of Qld, 4072, Australia.
Uni Ph +61 7 365 4184, Home 391 7727, Fax 365 1999