Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive functions are rather slow #1067

Open
rdbyk opened this issue Dec 30, 2023 · 4 comments
Open

Recursive functions are rather slow #1067

rdbyk opened this issue Dec 30, 2023 · 4 comments
Milestone

Comments

@rdbyk
Copy link
Contributor

rdbyk commented Dec 30, 2023

function x = fib(n)
if n < 2
x = n;
else
x = fib(n-1) + fib(n-2);
end
endfunction

in Nelson 0.7.12.0

tic;fib(25);toc
Elapsed time is 20.999976 seconds.

in Balisc/Scilab

--> tic;fib(25);toc
ans =

0.5551460

-->

It is not important to me, but I was just a little bit surprised, anyway I really like the evolution of Nelson.

@Nelson-numerical-software
Copy link
Collaborator

Hi,
recursive functions will be fixed with version 2.0 (I began to rewrite current interpreter to increase compatibility and speed x100 on loop and recursive call ;) )
I will check on v1.x if it is possible to do better with existing interpreter. True performance will come with v2 :)

Contributions and feedbacks are welcome !!!

@Nelson-numerical-software
Copy link
Collaborator

fibonacci recursive algo can be converted to non-recursive:

function last_fibonacci = fib2(n)
    fib_sequence = zeros(1, n);
    fib_sequence(1) = 1;
    fib_sequence(2) = 1;

    for i = 3:n
        fib_sequence(i) = fib_sequence(i-1) + fib_sequence(i-2);
    end

    last_fibonacci = fib_sequence(end);
end
Elapsed time is 0.005784 seconds.
>> r

r =

       75025

@Nelson-numerical-software
Copy link
Collaborator

for v1.0 basic optimization:
image

@Nelson-numerical-software
Copy link
Collaborator

to test:

>> addpath('d:/')
>> timeit(@fib, 1, 25)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants