"jacob navia" <> wrote in message
news:forvf7$6jn$...
> 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