copying a vector? 
Author Message
 copying a vector?

I'm trying to make a distinct copy of a vector so that I can use vector-set!
in a function to create a new vector from the original without *changing*
the original vector.  To accomplish this so far, I've been making a "copy"
of the vector using:

(vector->list (list->vector original))

I just know there is a better/more efficient way to do this...? Can anyone
help me out?

I use petite chez scheme 6.  Thanks.

/TM



Thu, 12 Jun 2003 07:00:29 GMT  
 copying a vector?
Err... what I meant to say was

(list->vector (vector->list original))

/TM


Quote:
> I'm trying to make a distinct copy of a vector so that I can use
vector-set!
> in a function to create a new vector from the original without *changing*
> the original vector.  To accomplish this so far, I've been making a "copy"
> of the vector using:

> (vector->list (list->vector original))

> I just know there is a better/more efficient way to do this...? Can anyone
> help me out?

> I use petite chez scheme 6.  Thanks.

> /TM



Thu, 12 Jun 2003 07:29:17 GMT  
 copying a vector?
: I'm trying to make a distinct copy of a vector so that I can use vector-set!
: in a function to create a new vector from the original without *changing*
: the original vector.  To accomplish this so far, I've been making a "copy"
: of the vector using:

: (vector->list (list->vector original))

: I just know there is a better/more efficient way to do this...? Can anyone
: help me out?

The only thing I know of would be to create a function to make a
new vector the same size as the vector you want copied.  Then,
iterate through the elements in the vector and vector-set! the
elements in the copy vector to the contents vector.


(define vector-copy
  (lambda (vect)
    (let ((length (vector-length vect)))
      (do ((vect-cpy (make-vector length))
           (cnt 0))
        ((>= cnt length) vect-cpy)
        (vector-set! vect-cpy cnt (vector-ref vect cnt))
        (set! cnt (+ cnt 1))))))

--
Stephen Gordon



Thu, 12 Jun 2003 13:07:59 GMT  
 copying a vector?
I long ago created a utility function in my personal library called
vector-map which is like map except that it operates on vectors.

From vector-map it is easy to create vector-copy:

(define (vector-copy v) (vector-map (lambda (e) e) v))



Thu, 12 Jun 2003 17:21:37 GMT  
 copying a vector?
+---------------
| I'm trying to make a distinct copy of a vector...
|    (list->vector (vector->list original))
| I just know there is a better/more efficient way to do this...?
+---------------

Well, rather than spread that idiom throughout your code, I'd at least say:

        (define (vector-copy v)
          (list->vector (vector->list v)))

But, yes, that probably *is* the simplest/best way using the primitives
provided in the R5RS spec, since there's no "vector-copy" there. I suppose
you could that version with this one:

        (define (vector-copy old)
          (let* ((len (vector-length old))
                 (new (make-vector len)))
            (do ((i 0 (+ i 1)))
                ((>= i len))
              (vector-set! new i (vector-ref old i)))
            new))

However, this may or may not be faster and/or more space-efficient,
depending on the details of your system's garbage collector (e.g., is
it a generational/copying collector? -- in which case using mutation
might slow down future collections) or compiler (which might recognize
that idiom) or library (which might already have a "vector-copy" in it).

-Rob

-----

SGI Network Engineering         http://reality.sgi.com/rpw3/
1600 Amphitheatre Pkwy.         Phone: 650-933-1673
Mountain View, CA  94043        PP-ASEL-IA



Thu, 12 Jun 2003 11:46:04 GMT  
 copying a vector?

Quote:

> But, yes, that probably *is* the simplest/best way using the primitives
> provided in the R5RS spec, since there's no "vector-copy" there. I suppose
> you could that version with this one:

>         (define (vector-copy old)
>           (let* ((len (vector-length old))
>                  (new (make-vector len)))
>             (do ((i 0 (+ i 1)))
>                 ((>= i len))
>               (vector-set! new i (vector-ref old i)))
>             new))

Does this use something other than R5RS primitives?


Thu, 12 Jun 2003 23:08:06 GMT  
 copying a vector?

+---------------
| > But, yes, that probably *is* the simplest/best way using the primitives
| > provided in the R5RS spec, since there's no "vector-copy" there. I suppose
| > you could that version with this one:
| >
| >         (define (vector-copy old)
| >           (let* ((len (vector-length old))
| >                  (new (make-vector len)))
| >             (do ((i 0 (+ i 1)))
| >                 ((>= i len))
| >               (vector-set! new i (vector-ref old i)))
| >             new))
|
| Does this use something other than R5RS primitives?
+---------------

No, but it's more complicated and uses more mutation (side-effects)
than the simpler vector -> list -> vector version, and may run either
faster or slower than that version (for the various reasons I listed).
So unless it makes a significant difference (in a specific application
on a specific platform), I'd choose the simpler one.

-Rob

-----

SGI Network Engineering         http://reality.sgi.com/rpw3/
1600 Amphitheatre Pkwy.         Phone: 650-933-1673
Mountain View, CA  94043        PP-ASEL-IA



Fri, 13 Jun 2003 16:44:21 GMT  
 copying a vector?

Quote:
> ...
> So unless it makes a significant difference (in a specific application
> on a specific platform), I'd choose the simpler one.
> ...

I ran tests using each the four versions I've seen here, and the
simplest one does indeed appear be the best, at least w.r.t. Petite
Chez Scheme and Gambit-C.  Here are the Petite results and test code:

;;; Petite Chez Scheme Version 6.0a
;;; Copyright (c) 1998 Cadence Research Systems
;;; ...
;;; > (define tvs (make-random-vectors 1000 30))
;;; > (time (dotimes 10 (lambda () (map vector-copy-1 tvs))))
;;; Warning (time): thread system overhead skews timing results
;;; (time (dotimes 10 ...))
;;;     2 collections
;;;     160 ms elapsed cpu time, including 0 ms collecting
;;;     160 ms elapsed real time, including 0 ms collecting
;;;     2042632 bytes allocated, including 2022192 bytes reclaimed
;;; > (time (dotimes 10 (lambda () (map vector-copy-2 tvs))))
;;; (time (dotimes 10 ...))
;;;     2 collections
;;;     660 ms elapsed cpu time, including 0 ms collecting
;;;     660 ms elapsed real time, including 0 ms collecting
;;;     2240160 bytes allocated, including 2230608 bytes reclaimed
;;; > (time (dotimes 10 (lambda () (map vector-copy-3 tvs))))
;;; (time (dotimes 10 ...))
;;;     3 collections
;;;     830 ms elapsed cpu time, including 0 ms collecting
;;;     830 ms elapsed real time, including 0 ms collecting
;;;     3152024 bytes allocated, including 3163624 bytes reclaimed
;;; > (time (dotimes 10 (lambda () (map vector-copy-4 tvs))))
;;; (time (dotimes 10 ...))
;;;     2 collections
;;;     820 ms elapsed cpu time, including 0 ms collecting
;;;     820 ms elapsed real time, including 0 ms collecting
;;;     2565408 bytes allocated, including 2164440 bytes reclaimed

(define (vector-copy-1 v)
  (list->vector (vector->list v)))

(define (vector-copy-2 old)
  (let* ((len (vector-length old))
         (new (make-vector len)))
    (do ((i 0 (+ i 1)))
 ((>= i len))
      (vector-set! new i (vector-ref old i)))
    new))

(define vector-copy-3
  (lambda (vect)
    (let ((length (vector-length vect)))
      (do ((vect-cpy (make-vector length))
           (cnt 0))
   ((>= cnt length) vect-cpy)
        (vector-set! vect-cpy cnt (vector-ref vect cnt))
        (set! cnt (+ cnt 1))))))

(define (vector-copy-4 v) (vector-map (lambda (e) e) v))

;;;-------------------------------------------------------------------

(define vector-map
  (lambda (proc v)
    (let ((len (vector-length v)))
      (let ((new-v (make-vector len)))
        (let loop ((i 0))
          (if (= i len)
              new-v
              (begin
                (vector-set! new-v i (proc (vector-ref v i)))
                (loop (+ i 1)))))))))

(define dotimes
  (lambda (n operation)
    (do ((i 0 (1+ i)))
        ((= i n))
      (operation))))

(define (make-random-vectors n max-size)
  (define (make-random-vector max-size)
    (let* ((size (+ (random max-size) 1))
    (v (make-vector size)))
      (let loop ((i 0))
 (if (= i size)
     v
     (begin
       (vector-set! v i (random size))
       (loop (+ i 1)))))))
  (let loop ((i 0) (rvs '()))
    (if (= i n)
 rvs
 (loop (+ i 1) (cons (make-random-vector max-size) rvs)))))

;;; EOF



Sun, 15 Jun 2003 08:12:59 GMT  
 copying a vector?

Quote:



> > ...
> > So unless it makes a significant difference (in a specific application
> > on a specific platform), I'd choose the simpler one.
> > ...

> I ran tests using each the four versions I've seen here, and the
> simplest one does indeed appear be the best, at least w.r.t. Petite
> Chez Scheme and Gambit-C.  Here are the Petite results and test code:

Using Stalin, the simplest one is not best, by more than a factor of two.  However, I
can't be completely certain that some of the benchmark code wasn't optimized away
entirely, since it doesn't produce any output and I was too lazy to either inspect
the compiled code or modify it to produce output.  However, given the execution time,
it is clear at least some of the code was not optimized away.

---
hardware: dual CPU Pentium-III 500 MHz, 256 MB.
OS: Red Hat 6.2
gcc: egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

default flags:  stalin -On vector-copy-bench.scm
aggressive flags: stalin -Ob -Om -On -Or -Ot -copt -O3 -copt -fomit-frame-pointer
vector-copy-bench.scm

---
(define tvs (make-random-vectors 1000 30))
(dotimes 1000 (lambda () (map vector-copy-4 tvs)))
---


4.75user 0.44system 0:05.18elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (93major+18911minor)pagefaults 0swaps

2.13user 0.46system 0:02.59elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (93major+18910minor)pagefaults 0swaps

2.39user 0.42system 0:02.81elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (93major+18910minor)pagefaults 0swaps

2.25user 0.49system 0:02.74elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (93major+18910minor)pagefaults 0swaps

1.67user 0.54system 0:02.20elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (93major+18911minor)pagefaults 0swaps

0.45user 0.53system 0:00.98elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (92major+18911minor)pagefaults 0swaps

0.53user 0.46system 0:00.97elapsed 101%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (92major+18911minor)pagefaults 0swaps

0.56user 0.43system 0:00.98elapsed 101%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (92major+18911minor)pagefaults 0swaps



Tue, 17 Jun 2003 09:53:31 GMT  
 copying a vector?

Quote:

> hardware: dual CPU Pentium-III 500 MHz, 256 MB.

Slight correction (got my computers mixed up).  This one is:

hardware: dual CPU Pentium-II 400 MHz, 256 MB.



Tue, 17 Jun 2003 10:00:33 GMT  
 copying a vector?

I notice your directory is labeled "stalin-0.9".  When I cruise over to
Siskind's site, the contents of stalin.tar.Z are labeled "stalin-0.8".
What's this version 0.9?  Do tell...

Sent via Deja.com
http://www.deja.com/



Wed, 18 Jun 2003 00:28:13 GMT  
 copying a vector?

Quote:

> What's this version 0.9?  Do tell...

Uh, a typo?  :-)


Wed, 18 Jun 2003 01:39:04 GMT  
 copying a vector?

Quote:

> > What's this version 0.9?  Do tell...

> Uh, a typo?  :-)

"Release early, release often"

Oh yeah, This is Stalin...sigh

david rush
--
With guns, we are citizens. Without them, we are subjects.
        -- YZGuy, IPL



Sat, 21 Jun 2003 00:02:01 GMT  
 copying a vector?

Quote:



> > > What's this version 0.9?  Do tell...

> > Uh, a typo?  :-)

> "Release early, release often"

> Oh yeah, This is Stalin...sigh

yeah - "10 years without the right to exchange letters"...

IMHO, naming things after {*filter*}es truly sucks,
especially naming after the biggest {*filter*}er of his
fellow countrymen, ever.

Quote:
> david rush
> --
> With guns, we are citizens. Without them, we are subjects.

Subjects vs targets.
Quote:
>    -- YZGuy, IPL



Sat, 21 Jun 2003 10:14:01 GMT  
 
 [ 41 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. Distributions and integrals, and old copies of VECTOR

2. no vector = vector*vector in BLAS?

3. lack of vector-copy in r4rs

4. Problem with vector copy

5. copying vectors/arrays by value

6. Vector set!, void vector problem

7. matrix as vector of vectors

8. Inserting a vector slice into another vector

9. Optimal SSE code for dot producting 4 vectors with another vector for the Athlon

10. BLT vector creation: creation time grows as number of vectors created grows

11. (simple-vector 20) vs (vector single-float 20)

12. Vectors and Vector Spaces in ST80

 

 
Powered by phpBB® Forum Software