<p dir="ltr">Sounds like Racket is using a "deep binding" strategy rather than "shallow binding". I expect that SBCL uses shallow. <br>
</p>
<div class="gmail_quote">On Aug 16, 2013 5:10 AM, "Matthew Flatt" <<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
At Fri, 16 Aug 2013 13:59:55 +0400, Roman Klochkov wrote:<br>
><br>
> I compared parameterize with lexical var<br>
> ----<br>
> > (require rackunit)<br>
> > (define my-parameter (make-parameter (box 0)))<br>
> > (time<br>
> (parameterize ([my-parameter (box 0)])<br>
> (for ([x (in-range 10000000)])<br>
> (set-box! (my-parameter)<br>
> (add1 (unbox (my-parameter)))))<br>
> (check-equal? (unbox (my-parameter)) 10000000)))<br>
> cpu time: 3578 real time: 3610 gc time: 0<br>
> > (time<br>
> (let ([my-parameter (box 0)])<br>
> (for ([x (in-range 10000000)])<br>
> (set-box! my-parameter<br>
> (add1 (unbox my-parameter))))<br>
> (check-equal? (unbox my-parameter) 10000000)))<br>
> cpu time: 47 real time: 47 gc time: 0<br>
> ----<br>
><br>
> 100 times difference!<br>
><br>
> The same experiment with Common Lisp (SBCL):<br>
> ----<br>
> CL-USER> (setf *a* (list 0))<br>
> (0)<br>
> CL-USER> (time (progn (loop :for i :from 0 :below 10000000<br>
> :do (setf (car *a*) (+ 1 (car *a*)))) (= (car *a*) 10000000)))<br>
> Evaluation took:<br>
> 0.063 seconds of real time<br>
> 0.062500 seconds of total run time (0.062500 user, 0.000000 system)<br>
> 98.41% CPU<br>
> 172,464,541 processor cycles<br>
> 0 bytes consed<br>
> <br>
> T<br>
> CL-USER> (let ((a (list 0))) (time (loop :for i :from 0 :below 10000000<br>
> :do (setf (car a) (+ 1 (car a))))) (= (car a) 10000000))<br>
> <br>
> Evaluation took:<br>
> 0.047 seconds of real time<br>
> 0.046875 seconds of total run time (0.046875 user, 0.000000 system)<br>
> 100.00% CPU<br>
> 132,098,942 processor cycles<br>
> 0 bytes consed<br>
> <br>
> T<br>
> ----<br>
> Only 1.5 times.<br>
><br>
> Is it undesirable to use parameterize as replacement for common lisp special<br>
> variables? What is it designed for then?<br>
<br>
Parameters in Racket are grouped together in a an extra layer called a<br>
"parameterization", which enables capture of the current values of all<br>
parameters. For example, when a new thread is created in Racket, then<br>
the new inherits all of the current parameter values from the creating<br>
thread. A lack of cleverness in that layer is probably the main effect<br>
on performance in yuor example.<br>
<br>
Using a raw, symbol-keyed continuation mark would be closer to a Common<br>
Lisp special variable, I think. On my machine:<br>
<br>
;; parameter<br>
> (time<br>
(parameterize ([my-parameter (box 0)])<br>
(for ([x (in-range 10000000)])<br>
(set-box! (my-parameter)<br>
(add1 (unbox (my-parameter)))))<br>
(check-equal? (unbox (my-parameter)) 10000000)))<br>
cpu time: 2539 real time: 2537 gc time: 0<br>
<br>
;; direct<br>
> (time<br>
(let ([my-parameter (box 0)])<br>
(for ([x (in-range 10000000)])<br>
(set-box! my-parameter<br>
(add1 (unbox my-parameter))))<br>
(check-equal? (unbox my-parameter) 10000000)))<br>
cpu time: 45 real time: 45 gc time: 0<br>
<br>
;; raw continuation mark:<br>
> (time<br>
(let ([my-parameter<br>
(lambda ()<br>
(continuation-mark-set-first #f 'my-parameter))])<br>
(with-continuation-mark<br>
'my-parameter<br>
(box 0)<br>
(begin<br>
(for ([x (in-range 10000000)])<br>
(set-box! (my-parameter)<br>
(add1 (unbox (my-parameter)))))<br>
(check-equal? (unbox (my-parameter)) 10000000)))))<br>
cpu time: 244 real time: 243 gc time: 0<br>
<br>
That's still a fact of 5 difference. I expect that dynamic binding and<br>
special variables have played a more prominent role in Common Lisp than<br>
parameters or even continuation-mark lookup in Racket, and so it would<br>
make sense that more work has been done in the SBCL to make them fast.<br>
<br>
<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</blockquote></div>