Here a corrected code example with (collect-garbage) to make time measurement 
more deterministic and the results (still mutable-array-data is 50% slower than 
element-wise access):

#lang racket

(require math/array)

(define arr (mutable-array #[#[1. 2. 3.] #[4. 5. 6.]]))

(define (array->vector1 arr)
  (for/vector [(a (in-array arr))] a))

(define (array->vector2 arr)
  (mutable-array-data arr))

(define N 5000)

(collect-garbage)
(collect-garbage)
(collect-garbage)
(time (for [(i N)] (array->vector1 arr)))

(collect-garbage)
(collect-garbage)
(collect-garbage)
(time (for [(i N)] (array->vector2 arr)))


results:

racket arr-vec.rkt
cpu time: 778 real time: 780 gc time: 2
cpu time: 1271 real time: 1276 gc time: 4

Berthold


> On 11 May 2016, at 10:51, Berthold Bäuml <berthold.bae...@dlr.de> wrote:
> 
> Converting a mutable-array into a vector in Racket (not Typed Racket) seems 
> to be faster when done element-wise  instead of using mutable-array-data. Why 
> is this? I always thought crossing the untyped/typed boundary as little as 
> possible makes things run faster.
> 
> This little test program 
> 
> #lang racket
> (require math/array)
> 
> (define arr (mutable-array #[#[1. 2. 3.] #[4. 5. 6.]]))
> 
> (define (array->vector1 arr)
>  (for/vector [(a (in-array arr))] a))
> 
> (define (array->vector2 arr)
>  (mutable-array-data arr))
> 
> (define N 5000)
> (time (for [(i N)] (array->vector1 arr)))
> (time (for [(i N)] (array->vector2 arr)))
> 
> 
> results in
> 
> racket arr-vec.rkt   
> cpu time: 946 real time: 961 gc time: 58
> cpu time: 1270 real time: 1283 gc time: 4
> 
> Best,
> Berthold
> 
> -- 
> -----------------------------------------------------------------------
> Berthold Bäuml -- Head of Autonomous Learning Robots Lab
> DLR, Robotics and Mechatronics Center (RMC)
> Münchner Str. 20, D-82234 Wessling
> Phone +49 8153 282489
> http://www.robotic.de/Berthold.Baeuml
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
-----------------------------------------------------------------------
Berthold Bäuml -- Head of Autonomous Learning Robots Lab
DLR, Robotics and Mechatronics Center (RMC)
Münchner Str. 20, D-82234 Wessling
Phone +49 8153 282489
http://www.robotic.de/Berthold.Baeuml

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to