RE : crazy run-time fluctuations (mostly super-slow) -- a bit more data



Hi Rupert,

Thanks a lot!

I'm actually aware of copy-semantics versus reference-semantics
(at least I should --- I teach such things ;-)), and where
an example for a "copy-based" language is C++ (the default is
to think "copy"), while an example for a "reference-based" language
is Java (the default is to think "reference").

My mistake was just that I implicitly assumed that Lisp would be
copy-based, however it is reference-based! So I assumed the default
would be to copy, and for referencing/dereferencing special operators
would be used, while it is the opposite: the default is to handle
references, and for copying special operators are used.

Now that all becomes clear to me. It was that implicit assumption
which I carried around about Lisp (in Maxima the distinction is
rather blurred --- it has a lot of copy-semantics in it, and thus
likely I got that impression), and that together with all the other
problems (unknowns) created the confusion.

Thanks!

Oliver 

P.S. In another reply I'll use a slightly different way of storing
the random-state, which a find a bit nicer.

P.S.P.S. A meta-remark: I think top-posting in such e-mail replies is the best way to
handle it: the nice examples below are now clear, and the reply refers to the whole
thing.


On Sun, Oct 23, 2011 at 12:48:09PM +0100, Rupert Swarbrick wrote:
> Oliver Kullmann <O.Kullmann at swansea.ac.uk> writes:
> > However, with each invocation of :lisp seems to start a fresh
> > environment, unrelated to the invocation before, so that between two
> > Maxima-calls apparently no relation can be established between the
> > calls of random??:
> >
> > (%i447) :lisp (setq ss *random-state*)
> > #<random-state 000000000143b000>
> > (%i447) :lisp (setq ss2 *random-state*)
> > #<random-state 000000000143b000>
> > (%i447) :lisp (equal ss ss2)
> > T
> > (%i447) :lisp (random 1000 ss)
> > 808
> > (%i447) :lisp (random 1000 ss2)
> > 57
> > (%i447) :lisp (equal ss ss2)
> > T
> 
> If you're happy with C-like languages, the best thing is to think that
> ss and ss2 are like pointers to a (complicated) structure, which is also
> pointed to by *random-state*.
> 
> Thus your two calls to random are exactly the same as if you'd called
> (random 1000 ss) twice.
> 
> In common lisp, equality is a funny thing. You probably meant to use
> EQUAL-P here rather than EQUAL, but in fact you'll definitely get T
> every time because ss and ss2 are just pointing to the same structure.
> 
> See this transcript for a way to do what I think you want:
> 
> Maxima 5.25post http://maxima.sourceforge.net
> using Lisp SBCL 1.0.52.0.debian
> Distributed under the GNU Public License. See the file COPYING.
> Dedicated to the memory of William Schelter.
> The function bug_report() provides bug reporting information.
> (%i1) :lisp (defvar *ss1* (make-random-state t))
> 
> *SS1*
> (%i1) :lisp (defvar *ss2* (make-random-state t))
> 
> *SS2*
> (%i1) :lisp (setf *random-state* *ss1*)
> *** output flushed ***
> 
> (I'm throwing away the output because it's big)
> 
> (%i1) :lisp (defvar *frozen-ss1* (make-random-state nil))
> 
> *FROZEN-SS1*
> (%i1) :lisp (eq *ss1* *frozen-ss1*)
> 
> NIL
> (%i1) :lisp (equalp *ss1* *frozen-ss1*)
> 
> T
> 
> (This shows that the contents of *ss1* and *frozen-ss1* are the same (as
> you'd expect since *frozen-ss1* is supposed to be a copy of *ss1*). But
> eq returns NIL, since they aren't in the same place in memory. As a
> result when we run the next lines, *ss1* changes but *frozen-ss1* doesn't).
> 
> (%i1) :lisp (random 10)
> 
> 3
> (%i1) :lisp (random 10)
> 
> 6
> (%i1) :lisp (random 10)
> 
> 5
> (%i1) :lisp (random 10)
> 
> 7
> (%i1) :lisp (setf *random-state* *frozen-ss1*)
> *** output flushed ***
> (%i1) :lisp (random 10)
> 
> 3
> (%i1) :lisp (random 10)
> 
> 6
> (%i1) :lisp (random 10)
> 
> 5
> (%i1) :lisp (random 10)
> 
> 7
> (%i1) 
> 
> (Finally, let's check that *ss2* is just completely different.)
> 
> (%i1) :lisp (setf *random-state* *ss2*)
> *** output flushed ***
> (%i1) :lisp (random 10)
> 
> 8
> (%i1) :lisp (random 10)
> 
> 9
> (%i1) :lisp (random 10)
> 
> 5
> (%i1) :lisp (random 10)
> 
> 8
> 
> 
> 
> Tada! I hope this clears things up for you to some extent.
> 
> Rupert