Zelphir:
> I don't really understand the (in-list ...) thing. This seems to be
internal magic to me. Maybe it informs about the type of what is iterated
over and that makes it faster?

Pretty much. Here's a program that constructs a lot of random numbers and
adding them up in a few different ways.

#lang racket

(define random-list (build-list 500000 (λ (n) (random 100))))
(define random-vector (list->vector random-list))

(define (sum-generic xs)
  (for/sum ([i xs]) i))

(define (sum-list xs)
  (for/sum ([i (in-list xs)]) i))

(define (sum-vector xs)
  (for/sum ([i (in-vector xs)]) i))

(displayln "Sum a List: Generic vs Specific (with in-list)")
(time (sum-generic random-list))
(time (sum-list random-list))
(newline)

(displayln "Sum a Vector: Generic vs Specific (with in-vector)")
(time (sum-generic random-vector))
(time (sum-vector random-vector))
(newline)

(displayln "Specialisation limits applicability")
(sum-list random-vector)


Output:

Sum a List: Generic vs Specific (with in-list)
cpu time: 161 real time: 178 gc time: 0
24772117
cpu time: 33 real time: 41 gc time: 0
24772117

Sum a Vector: Generic vs Specific (with in-vector)
cpu time: 113 real time: 114 gc time: 0
24772117
cpu time: 36 real time: 40 gc time: 0
24772117

Specialisation limits applicability
. . in-list: contract violation
  expected: list?
  given: '#(12 44 14 8 10 81 33 56 78 32 95 43 96 34 52 46 59 5 29 86 91 44
38 87 28 25 62 85 46 4 39 44 19 47 76 53 14 17 11 47 55 47 55 58 31 82 15
51 23 21 25 91 82 30 86 99 99 91 94 93 75 40 28 61 23 28 40 65 58 62 14 87
72 67 94 8 81 76 1 10 3 48 95 7...

Dan

-- 
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