Implementation of recursion in different languages: what happens inthe background ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?S=E9bastien_de_Mapias?=

    Implementation of recursion in different languages: what happens inthe background ?

    Hello,
    Hopefully I'm asking my question on the proper forum, but it's some
    kind of low-level computer language issue and I guess here, many
    people are likely to give me fine enlightenments (and I suppose -?-
    that the interpreters I talk about below are coded in C)... I've found
    on the page

    a discussion about the performance differences between several
    languages (Ruby, Perl, Python...) to execute the same operation,
    using recursive function calls.

    Could someone explain me how such differences can be explained ?
    Where does it come from ?

    Thanks a lot...
    Regards,
    Seb
  • jbroquefere

    #2
    Re: Implementation of recursion in different languages: what happensin the background ?

    Hello,
    To my mind, it depends more about the compiler than the language.
    But, because im new here, and a young student, my intepretation might
    be wrong.



    On 12 fév, 12:13, "Sébastien de Mapias" <sglrig...@gmai l.comwrote:
    Hello,
    Hopefully I'm asking my question on the proper forum, but it's some
    kind of low-level computer language issue and I guess here, many
    people are likely to give me fine enlightenments (and I suppose -?-
    that the interpreters I talk about below are coded in C)... I've found
    on the pagehttp://antoniocangiano .com/2007/11/28/holy-shmoly-ruby-19-smokes-pyth...
    a discussion about the performance differences between several
    languages (Ruby, Perl, Python...) to execute the same operation,
    using recursive function calls.
    >
    Could someone explain me how such differences can be explained ?
    Where does it come from ?
    >
    Thanks a lot...
    Regards,
    Seb

    Comment

    • jacob navia

      #3
      Re: Implementation of recursion in different languages: what happensin the background ?

      Sébastien de Mapias wrote:
      Hello,
      Hopefully I'm asking my question on the proper forum, but it's some
      kind of low-level computer language issue and I guess here, many
      people are likely to give me fine enlightenments (and I suppose -?-
      that the interpreters I talk about below are coded in C)... I've found
      on the page

      a discussion about the performance differences between several
      languages (Ruby, Perl, Python...) to execute the same operation,
      using recursive function calls.
      >
      Could someone explain me how such differences can be explained ?
      Where does it come from ?
      >
      Thanks a lot...
      Regards,
      Seb
      Python 2.5.1: 31.507s
      Ruby 1.9.0: 11.934s

      I just added "C"

      not even 1 second...

      :-)
      #include <stdio.h>
      int fib(int n)
      {
      if (n < 2)
      return n;
      else
      return fib(n-1)+fib(n-2);
      }

      int main(void)
      {
      for(int i = 1; i<36;i++) {
      printf("fib(%d) =%d\n",i,fib(i) );
      }
      }
      fib(1)=1
      fib(2)=1
      fib(3)=2
      fib(4)=3
      fib(5)=5
      fib(6)=8
      fib(7)=13
      fib(8)=21
      fib(9)=34
      fib(10)=55
      fib(11)=89
      fib(12)=144
      fib(13)=233
      fib(14)=377
      fib(15)=610
      fib(16)=987
      fib(17)=1597
      fib(18)=2584
      fib(19)=4181
      fib(20)=6765
      fib(21)=10946
      fib(22)=17711
      fib(23)=28657
      fib(24)=46368
      fib(25)=75025
      fib(26)=121393
      fib(27)=196418
      fib(28)=317811
      fib(29)=514229
      fib(30)=832040
      fib(31)=1346269
      fib(32)=2178309
      fib(33)=3524578
      fib(34)=5702887
      fib(35)=9227465


      --
      jacob navia
      jacob at jacob point remcomp point fr
      logiciels/informatique

      Comment

      • Chris Dollin

        #4
        Re: Implementation of recursion in different languages: what happens in the background ?

        jacob navia wrote:
        Python 2.5.1: 31.507s
        Ruby 1.9.0: 11.934s
        >
        I just added "C"
        >
        not even 1 second...
        Since one expects a factor of between 10 and 100 for interpretive
        implementations , that's hardly surprising. How does your implementation
        perform for fib(100)?

        --
        "Well begun is half done." - Proverb

        Hewlett-Packard Limited Cain Road, Bracknell, registered no:
        registered office: Berks RG12 1HN 690597 England

        Comment

        • jacob navia

          #5
          Re: Implementation of recursion in different languages: what happensin the background ?

          Chris Dollin wrote:
          jacob navia wrote:
          >
          >Python 2.5.1: 31.507s
          >Ruby 1.9.0: 11.934s
          >>
          >I just added "C"
          >>
          >not even 1 second...
          >
          Since one expects a factor of between 10 and 100 for interpretive
          implementations , that's hardly surprising. How does your implementation
          perform for fib(100)?
          >
          Using long long instead of int and the 64 bit lcc-win compiler
          instead of the 32 bit lcc-win32 I obtain:
          fib(1)=1
          fib(2)=1
          fib(3)=2
          fib(4)=3
          fib(5)=5
          fib(6)=8
          fib(7)=13
          fib(8)=21
          fib(9)=34
          fib(10)=55
          fib(11)=89
          fib(12)=144
          fib(13)=233
          fib(14)=377
          fib(15)=610
          fib(16)=987
          fib(17)=1597
          fib(18)=2584
          fib(19)=4181
          fib(20)=6765
          fib(21)=10946
          fib(22)=17711
          fib(23)=28657
          fib(24)=46368
          fib(25)=75025
          fib(26)=121393
          fib(27)=196418
          fib(28)=317811
          fib(29)=514229
          fib(30)=832040
          fib(31)=1346269
          fib(32)=2178309
          fib(33)=3524578
          fib(34)=5702887
          fib(35)=9227465
          fib(36)=1493035 2
          fib(37)=2415781 7
          fib(38)=3908816 9
          fib(39)=6324598 6
          fib(40)=1023341 55
          fib(41)=1655801 41
          fib(42)=2679142 96
          fib(43)=4334944 37
          fib(44)=7014087 33
          fib(45)=1134903 170
          fib(46)=1836311 903
          fib(47)=2971215 073

          TimeThis : Command Line : tfib
          TimeThis : Start Time : Tue Feb 12 13:14:25 2008
          TimeThis : End Time : Tue Feb 12 13:16:51 2008
          TimeThis : Elapsed Time : 00:02:25.941

          I think no C compiler will go (using exactly the same program as given
          of course) beyond fib(50) in a reasonable amount of time.

          fib(100) would take more than the age of the Universe...


          --
          jacob navia
          jacob at jacob point remcomp point fr
          logiciels/informatique

          Comment

          • =?ISO-8859-1?Q?S=E9bastien_de_Mapias?=

            #6
            Re: Implementation of recursion in different languages: what happensin the background ?

            Could you please tell me how you 'timed' your
            execution ? Thanks.

            Comment

            • Chris Dollin

              #7
              Re: Implementation of recursion in different languages: what happens in the background ?

              jacob navia wrote:
              Chris Dollin wrote:
              >jacob navia wrote:
              >>
              >>Python 2.5.1: 31.507s
              >>Ruby 1.9.0: 11.934s
              >>>
              >>I just added "C"
              >>>
              >>not even 1 second...
              >>
              >Since one expects a factor of between 10 and 100 for interpretive
              >implementation s, that's hardly surprising. How does your implementation
              >perform for fib(100)?
              >>
              >
              Using long long instead of int and the 64 bit lcc-win compiler
              instead of the 32 bit lcc-win32 I obtain:
              Sorry, Jacob, I was confusing `fib` and `fact` and thinking of the
              size of the numbers rather than the time taken. The fix for the
              recursion time is easy.

              I'd say that you were comparing apples and oranges, except that's
              easy.

              --
              "Ashes are burning the way." - Renaissance, /Ashes Are Burning/

              Hewlett-Packard Limited registered no:
              registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

              Comment

              • Spiros Bousbouras

                #8
                Re: Implementation of recursion in different languages: what happensin the background ?

                On Feb 12, 11:13 am, "Sébastien de Mapias" <sglrig...@gmai l.com>
                wrote:
                Hello,
                Hopefully I'm asking my question on the proper forum, but it's some
                kind of low-level computer language issue and I guess here, many
                people are likely to give me fine enlightenments (and I suppose -?-
                that the interpreters I talk about below are coded in C)... I've found
                on the pagehttp://antoniocangiano .com/2007/11/28/holy-shmoly-ruby-19-smokes-pyth...
                a discussion about the performance differences between several
                languages (Ruby, Perl, Python...) to execute the same operation,
                using recursive function calls.
                >
                Could someone explain me how such differences can be explained ?
                Where does it come from ?
                I doubt that there is a simple answer to your
                question. One would have to dig deep in the
                internals of the interpreters/compilers of the
                languages under question to see how they
                implement function calls, arithmetic, etc.
                and how that might affect performance. So a
                group or mailing list for the developers of
                interpreters/compilers of the languages would
                be a better place.

                The only general comment I can make is that
                some languages optimize tail recursive calls
                by turning them into loops. Scheme for example
                is required to do that by its standard. I
                don't know what the situation is with Ruby and
                Python and it's not relevant to the Fibonacci
                benchmark since the recursive call there is
                not tail recursive.

                Crossposting and setting follow-ups for
                comp.programmin g where I think this is more
                on topic.

                Comment

                • Kaz Kylheku

                  #9
                  Re: Implementation of recursion in different languages: what happensin the background ?

                  On Feb 12, 4:19 am, jacob navia <ja...@nospam.c omwrote:
                  I think no C compiler will go (using exactly the same program as given
                  of course) beyond fib(50) in a reasonable amount of time.
                  >
                  fib(100) would take more than the age of the Universe...
                  fib(100) cannot be done using that program because the answer is the
                  69 bit integer 354224848179261 915075.

                  In a high level language, you can memoize your function with some
                  trivial spelling change.

                  In Python, simple memoization can be done with a custom function
                  decorator, which turns into a trivial blurb that you add in front of
                  the function definition.

                  In CLISP, with my own memoization macro:

                  [1](define-memoized-function fib (n) (if (< n 2) n (+ (fib (1- n))
                  (fib (- n 2)))))
                  [2](compile 'fib)
                  FIB ;
                  NIL ;
                  NIL
                  [3](time (fib 100))
                  Real time: 1.0E-6 sec.
                  Run time: 0.0 sec.
                  Space: 9944 Bytes
                  354224848179261 915075

                  You can do memoization and wider integers in C, but the program will
                  be so ugly that the Fibonacci part of it won't even be recognizeable
                  any longer, except for the name.


                  Comment

                  • Bartc

                    #10
                    Re: Implementation of recursion in different languages: what happens in the background ?


                    "jacob navia" <jacob@nospam.c omwrote in message
                    news:forvf7$6jn $1@aioe.org...
                    Sébastien de Mapias wrote:
                    >Hello,
                    >Hopefully I'm asking my question on the proper forum, but it's some
                    >kind of low-level computer language issue and I guess here, many
                    >people are likely to give me fine enlightenments (and I suppose -?-
                    >that the interpreters I talk about below are coded in C)... I've found
                    >on the page
                    >http://antoniocangiano.com/2007/11/2...s-python-away/
                    >a discussion about the performance differences between several
                    >languages (Ruby, Perl, Python...) to execute the same operation,
                    >using recursive function calls.
                    >>
                    >Could someone explain me how such differences can be explained ?
                    >Where does it come from ?
                    >>
                    >Thanks a lot...
                    >Regards,
                    >Seb
                    >
                    Python 2.5.1: 31.507s
                    Ruby 1.9.0: 11.934s
                    >
                    I just added "C"
                    >
                    not even 1 second...
                    >
                    :-)
                    #include <stdio.h>
                    int fib(int n)
                    {
                    if (n < 2)
                    return n;
                    else
                    return fib(n-1)+fib(n-2);
                    }
                    >
                    int main(void)
                    {
                    for(int i = 1; i<36;i++) {
                    printf("fib(%d) =%d\n",i,fib(i) );
                    }
                    }
                    That's not quite the same code as Ruby/Python, it should be more like:

                    #include <stdio.h>
                    int fib(int n)
                    {
                    if (n==0 || n==1) /* test this way */
                    return n;
                    else
                    return fib(n-1)+fib(n-2);
                    }

                    int main(void)
                    {
                    for(int i = 0; i<36;i++) { /* start from 0 */
                    printf("fib(%d) =%d\n",i,fib(i) );
                    }
                    }

                    Your changes gave you an advantage of some 10% :-)

                    For the record, my own timings were: Ruby(1.8.6) 90 secs, Python(2.5.1) 30
                    secs, C(lccwin) 0.52 seconds.

                    (And one of my own compiled C-like languages, 0.5 seconds, one of my own
                    interpreted languages, 10 secs. If new Ruby is really that fast then I may
                    have a bit of work to do to stay ahead)

                    --
                    Bart


                    Comment

                    • Army1987

                      #11
                      Re: Implementation of recursion in different languages: whathappens in the background ?

                      jacob navia wrote:
                      I think no C compiler will go (using exactly the same program as given
                      of course) beyond fib(50) in a reasonable amount of time.
                      army1987@army19 87-laptop:~$ cat fib.c
                      #include <stdio.h>
                      long long fib(long long n)
                      {
                      if (n < 2)
                      return n;
                      else
                      return fib(n-1)+fib(n-2);
                      }

                      int main(void)
                      {
                      for(long long i = 1; i<52;i++) {
                      printf("fib(%ll d)=%lld\n",i,fi b(i));
                      }
                      }
                      army1987@army19 87-laptop:~$ gcc -std=c99 -O3 fib.c -o fib
                      army1987@army19 87-laptop:~$ time ./fib
                      fib(1)=1
                      fib(2)=1
                      [...]
                      fib(50)=1258626 9025
                      fib(51)=2036501 1074

                      real 19m4.878s
                      user 18m50.311s
                      sys 0m3.824s

                      (No, it's not reasonable...)
                      (BTW, what does it do when n < 0?)
                      --
                      Army1987 (Replace "NOSPAM" with "email")

                      Comment

                      • jacob navia

                        #12
                        Re: Implementation of recursion in different languages: what happensin the background ?

                        Army1987 wrote:
                        jacob navia wrote:
                        >
                        >I think no C compiler will go (using exactly the same program as given
                        >of course) beyond fib(50) in a reasonable amount of time.
                        army1987@army19 87-laptop:~$ cat fib.c
                        [snip]
                        fib(51)=2036501 1074
                        >
                        real 19m4.878s
                        user 18m50.311s
                        sys 0m3.824s
                        >
                        (No, it's not reasonable...)
                        (BTW, what does it do when n < 0?)
                        Off by one bug?

                        :-)

                        Try fib(55) when your computer has nothing else to do.

                        In any case, the non recursive function will do this
                        in MUCH less than a second.

                        When it is less than zero it returns its argument
                        unchanged...

                        --
                        jacob navia
                        jacob at jacob point remcomp point fr
                        logiciels/informatique

                        Comment

                        • Spoon

                          #13
                          Re: Implementation of recursion in different languages: what happensin the background ?

                          Army1987 wrote:
                          (And you can use a floating type instead of long int to have a wider
                          range, if you don't care about results becoming inexact for large n...)
                          I offer another fast (and not quite correct) implementation.

                          #include <stdint.h>
                          uint64_t fib(int n)
                          {
                          uint64_t temp, u = 0, v = 1;
                          while (--n) { temp = u + v; u = v; v = temp; }
                          return v;
                          }

                          Comment

                          • Army1987

                            #14
                            Re: Implementation of recursion in different languages: whathappens in the background ?

                            jacob navia wrote:
                            Army1987 wrote:
                            [fibonacci function]
                            >(BTW, what does it do when n < 0?)
                            >
                            Off by one bug?
                            No. If you use a signed parameter for fib(), you ought to look up how
                            fibonacci(-n) is defined. Another possibility is to use an unsigned
                            parameter.
                            :-)
                            >
                            Try fib(55) when your computer has nothing else to do.
                            >
                            In any case, the non recursive function will do this in MUCH less than a
                            second.
                            Indeed.

                            <OT>
                            army1987@army19 87-laptop:~$ cat fib.py
                            #!/usr/bin/python
                            def fib(n):
                            a, b = 0, 1
                            for i in xrange(n):
                            a, b = b, a + b
                            return a

                            if __name__ == "__main__":
                            for i in xrange(101):
                            print i, fib(i)

                            army1987@army19 87-laptop:~$ time ./fib.py
                            0 0
                            1 1
                            [...]
                            99 218922995834555 169026
                            100 354224848179261 915075

                            real 0m0.016s
                            user 0m0.016s
                            sys 0m0.000s
                            </OT>
                            I know there is an implementation which gives exact results without using
                            floating point with O(log n) time.

                            --
                            Army1987 (Replace "NOSPAM" with "email")

                            Comment

                            • Army1987

                              #15
                              Re: Implementation of recursion in different languages: whathappens in the background ?

                              Spoon wrote:
                              Army1987 wrote:
                              I offer another fast (and not quite correct) implementation.
                              >
                              #include <stdint.h>
                              uint64_t fib(int n)
                              {
                              uint64_t temp, u = 0, v = 1;
                              while (--n) { temp = u + v; u = v; v = temp; }
                              return v;
                              }
                              It's not correct only when n <= 0.
                              (When fibonacci(n) is greater than the number of grains of wheat on that
                              chessboard, the result is not correct in Z, but it *is* correct in Z_{2^64}.)


                              --
                              Army1987 (Replace "NOSPAM" with "email")

                              Comment

                              Working...