My message below is not doing the right test. This is the right test
and it produces the expected result, namely that the TR version runs a
bit faster (presumably because of type-based optimizations that TR
does).

Robby

#lang racket

(module t typed/racket/base
  (: divides? : Integer Integer -> Boolean)
  (define (divides? a b)
    (cond [(zero? a)  #f]
          [else  (= (remainder b a) 0)]))
  (provide divides?))

(module c racket/base
  (require racket/contract/base)
  (define (divides? a b)
    (cond [(zero? a)  #f]
          [else  (= (remainder b a) 0)]))
  (provide
   (contract-out
    [divides? (-> exact-integer? exact-integer? boolean?)])))

(require (prefix-in c: (submod "." c)))
(require (prefix-in t: (submod "." t)))

(time
 (for ([x (in-range 3 5000000)])
   (if (3 . c:divides? . x)
       x
       0)))

(time
 (for ([x (in-range 3 5000000)])
   (if (3 . t:divides? . x)
       x
       0)))


On Mon, Aug 24, 2015 at 8:18 AM, Robby Findler
<[email protected]> wrote:
> It looks to me like the slowdown isn't entirely explained by contract
> checking, or perhaps TR isn't generating the contracts I would have
> guessed. With the program below, I see this output
>
> cpu time: 1228 real time: 1228 gc time: 133
> cpu time: 658 real time: 658 gc time: 18
> cpu time: 80 real time: 81 gc time: 0
>
> but would have expected the first two lines to be nearly the same.
>
> Robby
>
> #lang racket
>
> (require (only-in math/number-theory divides?))
>
> (define (divisible-by? x d)
>   (= (modulo x d)
>      0))
>
> (module d racket/base
>   (require racket/contract/base)
>   (define (divisible-by? x d)
>     (= (modulo x d)
>        0))
>   (provide
>    (contract-out
>     [divisible-by? (-> exact-integer? exact-integer? boolean?)])))
>
> (require (prefix-in c: (submod "." d)))
>
>
> (module+ main
>   (time
>    (for ([x (in-range 3 5000000)])
>      (if (3 . divides? . x)
>          x
>          0)))
>
>   (time
>    (for ([x (in-range 3 5000000)])
>      (if (3 . c:divisible-by? . x)
>          x
>          0)))
>
>   (time
>    (for ([x (in-range 3 5000000)])
>      (if (x . divisible-by? . 3)
>          x
>          0))))

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to