[racket] first vs car ; last vs cdr ; empty? vs null?

From: Greg Hendershott (greghendershott at gmail.com)
Date: Sat Mar 8 13:35:11 EST 2014

As for length:

(for ([i 3]) (collect-garbage))
(let ((ls (time (make-list 50000000 0))))
  (time (length ls))
  (time (length ls))
  (time (length ls))
  (void))

It's what I would expect:

cpu time: 6733 real time: 6744 gc time: 6419
cpu time: 141 real time: 141 gc time: 0
cpu time: 145 real time: 145 gc time: 0
cpu time: 142 real time: 142 gc time: 0

- - - - -

Next, thinking about 50000000 elements made me think about vectors,
vector? and vector-length.

This:

(for ([i 3]) (collect-garbage))
(let ((v (time (make-vector 50000000 0))))
  (time (vector-length v))
  (time (vector-length v))
  (time (vector-length v))
  (void))

prints:

cpu time: 182 real time: 183 gc time: 1
cpu time: 315 real time: 315 gc time: 314
cpu time: 0 real time: 0 gc time: 0
cpu time: 0 real time: 0 gc time: 0

Huh?  The first call to vector-length takes even longer than making
the vector (and, seems to be almost entirely gc).

I would have guessed that make-vector would create a structure with
the length already stored in it, and all vector-length calls would be
essentially 0.

Posted on the users mailing list.