• fibonacci in C, the speed of 3 implementations

    From Rosario19@3:633/10 to All on Wed Apr 22 11:35:16 2026

    I dont remember if this was already written here...

    there are 3 fibonacci functions: fib (fibonacci 2 recursive), fib1
    (fibonacci 1 recursive), and f (fibunacci iterative) -------------------------------------------------
    #include <stdio.h>
    #include <time.h>

    unsigned fib(unsigned n){if(n<2)return n; return fib(n-1)+fib(n-2);}

    unsigned fibr(unsigned i, unsigned j, unsigned n)
    {if(n==0) return i;
    return fibr(j,i+j,n-1);
    }

    unsigned fib1(unsigned n){return fibr(0,1,n);}

    unsigned f(unsigned n)
    {unsigned r,i,j;
    if(n==0) return 0;
    for(r=j=i=1,n-=1;n;--n)
    {r=j;j+=i;i=r;}
    return r;
    }


    main(void)
    {time_t in,fn;
    double d;
    unsigned i, r, j;

    in=time(0);
    for(i=0;i<40;++i)
    {r=fib(i);
    //printf("fibonacciRic(%u)=%u\n", i, r);
    }
    fn=time(0); d=difftime(fn,in); printf("DeltaTime Fib 2 recursive: %f
    \n", d);

    in=time(0);
    for(j=0;j<1000000;++j)
    for(i=0;i<40;++i)
    {r=fib1(i);
    //printf("fibonacciRic(%u)=%u\n", i, r);
    }
    fn=time(0); d=difftime(fn,in); printf("DeltaTime Fib 1 recursive: %f
    \n", d);

    in=time(0);
    for(j=0;j<1000000;++j)
    for(i=0;i<40;++i)
    {r=f(i);
    //printf("fibonacciIter(%u)=%u\n", i, r);
    }
    fn=time(0); d=difftime(fn,in); printf("DeltaTime Fib iterative: %f
    \n", d);

    return 0;
    }
    --------------

    results

    fibonacci
    DeltaTime Fib 2 recursive: 4.000000
    DeltaTime Fib 1 recursive: 5.000000
    DeltaTime Fib iterative: 2.000000

    so fib 2 recursive is about 1,000,000 times more slow than the fib 1
    recursive

    and fib iterative is 2x more fast than the fib 1 recursive...

    right?

    in assembly I have seen that here j is created and the loop extern
    shoul be run, the same the one inner


    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael S@3:633/10 to All on Wed Apr 22 14:12:11 2026
    On Wed, 22 Apr 2026 11:35:16 +0200
    Rosario19 <Ros@invalid.invalid> wrote:

    I dont remember if this was already written here...

    there are 3 fibonacci functions: fib (fibonacci 2 recursive), fib1
    (fibonacci 1 recursive), and f (fibunacci iterative) -------------------------------------------------
    #include <stdio.h>
    #include <time.h>

    unsigned fib(unsigned n){if(n<2)return n; return fib(n-1)+fib(n-2);}

    unsigned fibr(unsigned i, unsigned j, unsigned n)
    {if(n==0) return i;
    return fibr(j,i+j,n-1);
    }

    unsigned fib1(unsigned n){return fibr(0,1,n);}

    unsigned f(unsigned n)
    {unsigned r,i,j;
    if(n==0) return 0;
    for(r=j=i=1,n-=1;n;--n)
    {r=j;j+=i;i=r;}
    return r;
    }


    main(void)
    {time_t in,fn;
    double d;
    unsigned i, r, j;

    in=time(0);
    for(i=0;i<40;++i)
    {r=fib(i);
    //printf("fibonacciRic(%u)=%u\n", i, r);
    }
    fn=time(0); d=difftime(fn,in); printf("DeltaTime Fib 2 recursive: %f
    \n", d);

    in=time(0);
    for(j=0;j<1000000;++j)
    for(i=0;i<40;++i)
    {r=fib1(i);
    //printf("fibonacciRic(%u)=%u\n", i, r);
    }
    fn=time(0); d=difftime(fn,in); printf("DeltaTime Fib 1 recursive: %f
    \n", d);

    in=time(0);
    for(j=0;j<1000000;++j)
    for(i=0;i<40;++i)
    {r=f(i);
    //printf("fibonacciIter(%u)=%u\n", i, r);
    }
    fn=time(0); d=difftime(fn,in); printf("DeltaTime Fib iterative: %f
    \n", d);

    return 0;
    }
    --------------

    results

    fibonacci
    DeltaTime Fib 2 recursive: 4.000000
    DeltaTime Fib 1 recursive: 5.000000
    DeltaTime Fib iterative: 2.000000

    so fib 2 recursive is about 1,000,000 times more slow than the fib 1 recursive

    and fib iterative is 2x more fast than the fib 1 recursive...

    right?

    in assembly I have seen that here j is created and the loop extern
    shoul be run, the same the one inner


    Since you don't use results of calculations and since compiler can see
    the bodies of the functions fib(), fibr(), fib1() and f() and can
    figure out that they have no side effects, I would expect that good
    optimizing compiler will not call this functions at all.


    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Thu Apr 23 11:53:22 2026
    Am 22.04.2026 um 13:12 schrieb Michael S:

    Since you don't use results of calculations and since compiler can
    see the bodies of the functions fib(), fibr(), fib1() and f() and
    can figure out that they have no side effects, I would expect that
    good optimizing compiler will not call this functions at all.
    With a *very* good compiler you can help the compiler to do that with constexpr. ;-)

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)