This algorithm is quite fast and corect:
public static int fibo(int n) {
int t = 1;
int result = 0;
while (n-- > 0) {
result = t - result;
t = t + result;
}
return result;
}
Am too busy to convert the code to Go, but it should be easy.
Am Freitag, 25. Januar 2019 05:48:54 UTC+1 schrieb Ian Lance Taylor:
>
> On Thu, Jan 24, 2019 at 7:01 PM Karthik Krishnaswamy
> <[email protected] <javascript:>> wrote:
> >
> > I am just curious to understand what is the best possible way to
> increase the execution speed of this particular program ? I am still
> learning go though :)
>
> To speed up that particular program, don't use recursive Fibonacci.
> The recursive function that you wrote has complexity O(2 ** N). Write
> a simple loop, complexity O(N).
>
> Ian
>
>
> > On Fri, Jan 25, 2019 at 8:26 AM Ian Lance Taylor <[email protected]
> <javascript:>> wrote:
> >>
> >> On Thu, Jan 24, 2019 at 6:21 PM Topget <[email protected]
> <javascript:>> wrote:
> >> >
> >> > I have tested several simple functions with Golang and Java. To my
> surprise, Java sometimes is faster than Golang(especially in recursive
> function and some function in standard library such as math/rand.Rand). I
> wonder why. Here is some code I used for test and the result.
> >>
> >> Because goroutines start with a small stack that grows as needed,
> >> deeply recursive functions will tend to have somewhat worse behavior
> >> the first time they are called.
> >>
> >> Ian
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> >> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected] <javascript:>.
> >> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" 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.