Re: [racket] Style and Performance question

2011-05-11 Thread Patrick King
Thanks for the summary. I was going to post something similar shortly. As always, thanks to the community for your help. Pat On May 10, 2011 7:51 PM, "Matthew Flatt" wrote: I think all of your questions have been covered in the thread, but I'll answer them again to offer a few extra opinions an

Re: [racket] Style and Performance question

2011-05-10 Thread Matthew Flatt
I think all of your questions have been covered in the thread, but I'll answer them again to offer a few extra opinions and to summarize the discussion... At Mon, 9 May 2011 18:38:41 -0500, Patrick King wrote: > I now show the "approved" solution, and my own, both edited a bit to cleanly > present

Re: [racket] Style and Performance question

2011-05-10 Thread Matthias Felleisen
On May 10, 2011, at 3:40 PM, Matthew Flatt wrote: > At Tue, 10 May 2011 10:05:35 -0400, Matthias Felleisen wrote: >> 2. The addition in lieu of the multiplication is consistently the fastest >> version of the three I mentioned last night: >> >> (: the-sqrt : Real -> Real) >> (define (the-sqr

Re: [racket] Style and Performance question

2011-05-10 Thread Matthew Flatt
At Tue, 10 May 2011 10:05:35 -0400, Matthias Felleisen wrote: > 2. The addition in lieu of the multiplication is consistently the fastest > version of the three I mentioned last night: > > (: the-sqrt : Real -> Real) > (define (the-sqrt y) > (let loop [(x (/ y 2.0))] > (let [(error

Re: [racket] Style and Performance question

2011-05-10 Thread Matthias Felleisen
1. I reran the timing experiments: build a list of 1,000 random naturals in [0,10) run a loop 100 times that computes the sqrt for the entire list for each iteration The rough results are: -- untyped, exact rationals: ~10,000 msec -- untyped, inexact numb

Re: [racket] Style and Performance question

2011-05-10 Thread Sam Tobin-Hochstadt
On Tue, May 10, 2011 at 6:03 AM, Matthias Felleisen wrote: > When I converted it, the type checker asked for something other than x and I > realized that I needed to get an inexact in somewhere. " What error did the typechecker give you? The exact-rational version type checks fine for me. -- s

Re: [racket] Style and Performance question

2011-05-10 Thread Sam Tobin-Hochstadt
On Mon, May 9, 2011 at 11:37 PM, Robby Findler wrote: > I don't think that this is accurate, Sam. You're totally right. The "optimization" I suggested is not correct (and TR doesn't do it), and the speedup is almost entirely due to using floats, not to using TR. I was pretty confused there. --

Re: [racket] Style and Performance question

2011-05-10 Thread Matthias Felleisen
Robby is right. I fell into the exact-rational-arithmetic trap. Racket "silently" crunched exact fractions for the original version. When I converted it, the type checker asked for something other than x and I realized that I needed to get an inexact in somewhere. "2.0" was the natural place. I

Re: [racket] Style and Performance question

2011-05-09 Thread Robby Findler
On Mon, May 9, 2011 at 10:25 PM, Sam Tobin-Hochstadt wrote: > On Mon, May 9, 2011 at 11:19 PM, Robby Findler > wrote: >> Even without typed racket, this: >> >> (define (my-sqrt y) >>  (let loop [(x (/ y 2.0))] > >> is much faster than this: >> >> (define (my-sqrt y) >>  (let loop [(x (/ y 2))] >

Re: [racket] Style and Performance question

2011-05-09 Thread Sam Tobin-Hochstadt
On Mon, May 9, 2011 at 11:19 PM, Robby Findler wrote: > Even without typed racket, this: > > (define (my-sqrt y) >  (let loop [(x (/ y 2.0))] > is much faster than this: > > (define (my-sqrt y) >  (let loop [(x (/ y 2))] Yes, this is one of the optimizations TR does. :) As to why TR is so much

Re: [racket] Style and Performance question

2011-05-09 Thread Robby Findler
Even without typed racket, this: (define (my-sqrt y) (let loop [(x (/ y 2.0))] (let [(error (- y (* x x)))] (if (< (abs error) epsilon) x (loop (+ x (/ error 2 x))) is much faster than this: (define (my-sqrt y) (let loop [(x (/ y 2))] (let [(error (- y (

Re: [racket] Style and Performance question

2011-05-09 Thread Matthias Felleisen
[Sam, Vincent: I have no clue how TR can beat R by two orders of magnitude. See end of message.] I wrote a little function to test your two implementations of sqrt on lists of random numbers: (define (time-and-compare LARGE) (define n (build-list LARGE (lambda (_) (random 10 (collect-g

[racket] Style and Performance question

2011-05-09 Thread Patrick King
I was working through a problem at Programming Praxis, and was happy to find that my solution closely matched the "approved" solution, but I noted a fairly big difference in our styles, and I thought I'd seek the community's feedback.