[plt-scheme] redefining set!

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Mon Jun 18 18:27:32 EDT 2007

Hans Oesterholt-Dijkema skrev:
> Hmm, I'm not sure if I'm going to win a lot with this
> excersize:
> 
>  > (define (f x) (if (> x 0) (begin (g x) (f (- x 1))) #t))
>  > (define (g x) x)
>  > (time (f 10000000))
> cpu time: 1572 real time: 1572 gc time: 0
> #t
>  > (define v (vector g))
>  > (define (h x) (if (> x 0) (begin ((vector-ref v 0) x) (h (- x 1))) #t))
>  > (time (h 10000000))
> cpu time: 2023 real time: 2033 gc time: 0   (123%)
> #t
>  > (time (h 10000000))
> cpu time: 1903 real time: 1913 gc time: 0  (117%)
> #t
>  > (define k (make-hash-table))
>  > (hash-table-put! k 'g g)
>  > (define (l x) (if (> x 0) (begin ((hash-table-get k 'g) x) (l (- x 
> 1))) #t))
>  > (time (l 10000000))
> cpu time: 2113 real time: 2123 gc time: 0  (130%)
> 
>  > (define-syntax q
> (syntax-rules ()
> ((_ g a1 ...) (g a1 ...))))
>  > (define (z x) (if (> x 0) (begin (q g x) (z (- x 1))) #t))
>  > (time (z 10000000))
> cpu time: 1663 real time: 1663 gc time: 0  (100%)
> #t
>  > (time (f 10000000))
> cpu time: 1632 real time: 1632 gc time: 0
> #t

Good idea to bechmark. Are you using DrScheme?

Remember

   (require mzscheme)

at the top in order to help the inliner.

Also disable debug-info in the language menu.


(require mzscheme)

(define (f x) (if (> x 0) (begin (g x) (f (- x 1))) #t))
(define (g x) x)
(time (f 10000000))

(define v (vector g))
(define (h x) (if (> x 0) (begin ((vector-ref v 0) x) (h (- x 1))) #t))
  (time (h 10000000))

(define k (make-hash-table))
(hash-table-put! k 'g g)
(define (l x) (if (> x 0) (begin ((hash-table-get k 'g) x) (l (- x 1))) #t))

(define-syntax q
   (syntax-rules ()
     ((_ g a1 ...) (g a1 ...))))
(define (z x) (if (> x 0) (begin (q g x) (z (- x 1))) #t))
(time (z 10000000))
(time (f 10000000))


No (require mzscheme) and with debug:

   cpu time: 2516 real time: 2640 gc time: 0
   cpu time: 3187 real time: 3250 gc time: 0
   cpu time: 3000 real time: 3015 gc time: 0
   cpu time: 2562 real time: 2625 gc time: 0

With (require mzscheme) and with debug:

   cpu time: 1578 real time: 1672 gc time: 0
   cpu time: 2172 real time: 2250 gc time: 0
   cpu time: 1953 real time: 1954 gc time: 0
   cpu time: 1625 real time: 1656 gc time: 0

With (require mzscheme) and no debug:

   cpu time: 750 real time: 813 gc time: 0
   cpu time: 781 real time: 796 gc time: 0
   cpu time: 735 real time: 735 gc time: 0
   cpu time: 750 real time: 750 gc time: 0

Now the times are almost identical.

-- 
Jens Axel Søgaard




Posted on the users mailing list.