; vectormap.scm -
; vector mapping functions
; the supposedly slower one is actually faster
;(time-apply proc arg-list) invokes the procedure proc with the arguments in arg-list. Four ;values are returned: a list containing the result(s) of applying thunk, the number of ;milliseconds of CPU time required to obtain this result, the number of ``real'' milliseconds ;required for the result, and the number of milliseconds of CPU time (included in the second ;result) spent on garbage collection.
;;; The fast version
(define vmap-1
(lambda (proc v)
(let ((len (vector-length v)))
(let ((new-v (make-vector len)))
(let lp ((i 0))
(if (= i len)
new-v
(begin
(vector-set! new-v i (proc (vector-ref v i)))
(lp (+ i 1)))))))))
;;;; Slower and simpler...
(define vmap
(lambda (proc v)
(list->vector
(map proc (vector->list v)))))
(define (range from to)
(if (> from to)
'()
(cons from (range (+ from 1) to))))
(define (do-times n proc)
(for-each proc
(range 1 n)))
;(do-times 100 (lambda (x) (display (format "~A," x))))
(define (do-times-timed n proc)
(define-values (results cputime realtime garbagetime)
(time-apply (lambda () (do-times n proc)) '()))
(display (format "~% ~A ~A ~A"
(exact->inexact (/ cputime n))
(exact->inexact (/ realtime n))
(exact->inexact (/ garbagetime n)) )))
;-----------------------------------------------------------------------
(define long-vector (eval (cons 'vector (range 1 1000))))
; supposedly faster one:
(do-times-timed 10000 (lambda (y) (vmap-1 (lambda (x) (* 2 x)) long-vector)))
;9.771 9.771 0.0
; supposedly slower one:
(do-times-timed 10000 (lambda (y) (vmap (lambda (x) (* 2 x)) long-vector)))
;4.268 4.268 0.0
;-----------------------------------------------------------------------
; multiply a vector by a scalar
;(define args-list '())
;(define long-vector (eval (cons 'vector (range 1 1000))))
;(define-values (results cputime realtime garbagetime)
; (time-apply (lambda () (vmap-1 (lambda (x) (* 2 x)) long-vector)) args-list))
;(display (format "~%~A, ~A, ~A" cputime realtime garbagetime))
Text file Source (historic): geocities.com/soho/square/3472
geocities.com/soho/squaregeocities.com/soho
(to report bad content: archivehelp @ gmail)
|
|
|
|
|