equivalent to your explicit function, but slow on my computer
timespacex '({: , +/)^:(474998) 1 1x '
63.3016 1.31469e6
Your's may be faster due to special code for inplace assignments.
but there may be shortcuts to finding a direct fib number.
----- Original Message -----
From: Jon Hough <[email protected]>
To: "[email protected]" <[email protected]>
Cc:
Sent: Tuesday, September 1, 2015 8:35 PM
Subject: Re: [Jprogramming] Comparing J speed
Apologies for the messed up fib2, I'm seriously considering dumping outlook.com.
> From: [email protected]
> To: [email protected]
> Date: Wed, 2 Sep 2015 01:32:45 +0100
> Subject: [Jprogramming] Comparing J speed
>
> In this talk https://www.youtube.com/watch?v=apBWkBDVlow
> the presenter attempts to show Haskell hasn't sacrificed speed for
> expressiveness by comparing a Java Fibonacci calculator to his Haskell
> one.(skip to the 18:00 mark).Essentially, to calculate the 475000th Fibonacci
> number, it took his Java program ~8 seconds, while the very terse Haskell
> program took ~6 seconds.
> So I tried to do the same in J. My first attempt, used a tacit, memoized verb
> fib1 =:1:`(($:@:<:) + ($:@:-&2))@.(2&<)M.
>
>
> However, this gives a stack error for large numbers (~100000). So I decided
> to make an imperative verb,
>
>
> fib2 =: 3 : 0 x1 =. x:1 x2 =. x:1 c =. 0 while. c < y do. tmp =. x1 x1 =. x2
> x2 =. tmp + x1 c=.c+1 end. x2
>
>
>
>
>
>
>
>
>
>
>
>
> )
>
>
> This gets there, I can calculate the 475000th Fibonacci number, but
>
>
> timespacex 'fib2 475000'
>
>
>
>
> 36.183 1.31558e6
>
>
> It takes 36 seconds (of course, my hardware is different to that in the
> presentation, but still...).
>
>
> Is there a speedier way to do this in J? Preferably a tacit one liner would
> also be good.
>
>
> Thanks,
> Jon
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm