On Tuesday, 26 August 2003 at  1:35:56 +0900, Brian Candler wrote:
> On Tue, Aug 26, 2003 at 12:40:45AM +0900, mark wrote:
> > Unless I'm mistaken n + sum(n-1) isn't a tail call since it has to add n to 
> > the result of sum(n-1) which prevents it from optimizing the call.
> > 
> > The following code runs without any errors, though it prints the wrong answer 
> > (probably due to overflow)
> > 
> > let rec sum n total = 
> > 	if n < 1 then total else sum (n-1) (total + n);;
> > 
> > print_int(sum 1000000 0);; 
> 
> Ah, thank you, and sorry for exposing my ignorance!
> 
> I was actually just trying to get some timings out. It seems ocaml is
> astoundingly fast for that test: 0.51s uncompiled, 0.37s compiled with
> ocamlc, versus 11.6s for the Ruby equivalent:
> 
>   def sum(n)
>     total = 0
>     n.times { |x| total += x }
>     total
>   end
> 
>   puts sum(1000000)
> 
> OTOH, the Ruby version gives the right answer :-)
> 

Hmm, I'm sitting here with a Sun box and a Linux box and
thought I'd try this myself. Here are the results:

Ruby:
time ruby tail
499999500000
1.250u 0.000s 0:01.23 101.6%    0+0k 0+0io 212pf+0w

Sun:
time ruby tail
499999500000
11.36u 0.05s 0:11.50 99.2%

BTW, both were built with the latest 1.8.0.


-- 
Jim Freeze
----------
Mother is far too clever to understand anything she does not like.
		-- Arnold Bennett