Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Disappointed at Python

Name: Anonymous 2011-11-09 19:55

Fibonacci number calculation

fibonacci.py:

def f(n):
    if n <= 1:
        return 1
    return f(n-1) + f(n-2)

print(f(30))


fibonacci.rb:

def f(n)
  if n <= 1
    return 1
  end
  return f(n-1) + f(n-2)
end

puts f(30)



$ python --version
Python 3.2.2
$ time python fibonacci.py
1346269

real    0m1.340s
user    0m1.043s
sys    0m0.035s
$ ruby1.8 --version
ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]
$ time ruby1.8 fibonacci.rb
1346269

real    0m1.539s
user    0m1.492s
sys    0m0.008s
$ ruby --version
ruby 1.9.2p290 (2011-07-09 revision 32553) [i386-darwin9.8.0]
$ time ruby fibonacci.rb
1346269

real    0m0.306s
user    0m0.296s
sys    0m0.007s


What the fuck going on, Guido?  I am switching to ruby.

Name: Anonymous 2011-11-09 20:13

I wish I was trolling, but Python (3 specially) is among the slowest languages in widespread use. CPython is quite mediocre and Guido and friends seemingly intend to keep it that way.

Name: Anonymous 2011-11-09 20:13

[code]
(21) unixs1 $ cat fib.java
public class fib
{
    public static void main(String args[])
    {
        System.out.print(fib(30));
    }

    public static int fib(int n)
    {
        return( n <= 1 ? 1 : fib(n-1) + fib(n-2));
    }
}
(22) unixs1 $ cat fib.py
def f(n):
        if n <= 1:
                return 1
        return f(n-1) + f(n-2)

print(f(30))
(23) unixs1 $ time java fib
1346269
real    0m0.329s
user    0m0.230s
sys     0m0.050s
(24) unixs1 $ time python fib.py
1346269

real    0m3.873s
user    0m3.660s
sys     0m0.040s

[code]

doesn't have ruby installed .

Name: Anonymous 2011-11-09 20:14

>>3

(21) unixs1 $ cat fib.java
public class fib
{
    public static void main(String args[])
    {
        System.out.print(fib(30));
    }

    public static int fib(int n)
    {
        return( n <= 1 ? 1 : fib(n-1) + fib(n-2));
    }
}
(22) unixs1 $ cat fib.py
def f(n):
        if n <= 1:
                return 1
        return f(n-1) + f(n-2)

print(f(30))
(23) unixs1 $ time java fib
1346269
real    0m0.329s
user    0m0.230s
sys     0m0.050s
(24) unixs1 $ time python fib.py
1346269

real    0m3.873s
user    0m3.660s
sys     0m0.040s



doesn't have ruby installed .

Name: Anonymous 2011-11-09 20:22

use python

Name: Anonymous 2011-11-09 20:31

not using pypy

Name: Anonymous 2011-11-09 20:31

not using pypy

Name: Anonymous 2011-11-09 20:31

not using pypy

Name: Anonymous 2011-11-09 20:31

not using pypy

Name: Anonymous 2011-11-09 20:31

not using pypy

Name: Anonymous 2011-11-09 20:31

not using pypy

Name: Anonymous 2011-11-09 20:35

>>6-11
Donate towards py3k in pypy
$4369 of $105000 (4.2%)
Good luck with that. Also it's not really that fast for a JIT.

Name: Anonymous 2011-11-09 21:57


(4) cabbage$ cat fib.c
#include<stdio.h>
int fib(int n)
{
  return(n <= 1 ? 1 : fib(n-1) + fib(n-2));
}

void main()
{
  fib(30);
}
(5) cabbage$ gcc fib.c -O2
(6) cabbage$ time ./a.out
real    0m0.017s
user    0m0.020s
sys     0m0.000s

Name: Anonymous 2011-11-09 22:18

$ cat > fib.py
def f(n):
    if n <= 1:
        return 1
    return f(n-1) + f(n-2)

print(f(30))
$ time python3 fib.py
1346269

real    0m0.712s
user    0m0.700s
sys    0m0.012s


$ cat > fib.c
#include <stdio.h>

int fib(int n) {
  return (n <= 1) ? 1 : fib(n - 1) + fib(n - 2);
}

int main(int argc, char **argv) {
  printf("%d\n", fib(30));
  return 0;
}
$ gcc fib.c -O3 -o fib
$ time ./fib
1346269

real    0m0.014s
user    0m0.012s
sys    0m0.000s


CL-USER> (defun fibonacci (n)
           (declare (optimize (speed 3))
                    (type fixnum n))
           (if (<= n 1)
               1
               (the fixnum (+ (fibonacci (1- n))
                              (fibonacci (- n 2))))))
FIBONACCI
CL-USER> (time (fibonacci 30))
Evaluation took:
  0.013 seconds of real time
  0.012001 seconds of total run time (0.012001 user, 0.000000 system)
  92.31% CPU
  28,334,732 processor cycles
  0 bytes consed



LITHP WINTH

Name: Anonymous 2011-11-10 1:44

% cat fib.hs
fibs = 0:1:zipWith (+) fibs (tail fibs)
main = print (fibs !! 31)
% ghc -O -o fib fib.hs
[1 of 1] Compiling Main             ( fib.hs, fib.o )
Linking fib ...
% time ./fib
1346269
./fib  0.00s user 0.00s system 0% cpu 0.002 total


Haskell wins.

Name: Anonymous 2011-11-10 2:16

>>1

That's because python is a crap, indirect threaded (switch-based) language interpreter.

Name: Anonymous 2011-11-10 3:10

You are measuring IO latencies, not code execution time. Please do not touch a programming language ever again.

Name: Anonymous 2011-11-10 6:06

>>17

bullshit

Name: Anonymous 2011-11-10 6:33

What's going on is that Ruby detects that the function is pure while Python does not, you can mend it yourself but I don't know if such behavior is advisable.

def pure (func):
    cache = {}
    def wrapper (*args):
        if args not in cache:
            cache[args] = func (*args)
        return cache[args]
    return wrapper

@pure
def fib (n):
    return 1 if n <= 1 else fib (n-1) + fib (n-2)

# Timing I/O is stupid
print fib (30)


[~/Python/trash] time python fib.py
1346269

real    0m0.167s
user    0m0.133s
sys    0m0.034s


I don't have Ruby installed so I can't test whether it still is faster on my machine. I also don't have Python3k installed on my machine so I had to run this using "CPython version 2.7".

Name: Anonymous 2011-11-10 6:34

>>19
One could probably argue that the function is no longer pure since it mutates its cache.

Name: OP 2011-11-10 7:58

With monkey-around code by >>19, I got

$ time python fibonacci.py

real    0m0.088s
user    0m0.060s
sys    0m0.018s


This is faster than ruby 1.9.

$ time ruby fibonacci.rb

real    0m0.310s
user    0m0.297s
sys    0m0.008s


If ruby was detecting pureness of functions, it did a poor job.

Name: Anonymous 2011-11-10 8:25

>>21
If ruby was detecting pureness of functions, it did a poor job.
Keep in mind that Ruby is slow as fuck.

Name: Anonymous 2011-11-10 9:12

>>22
Actually it's quite a bit faster than FIOC in the general case¹.

¹http://shootout.alioth.debian.org/u32/which-programming-languages-are-fastest.php

Name: Anonymous 2011-11-10 10:01

>>21
Who are you calling ``Monkey'', MISTER?

Name: Anonymous 2011-11-10 10:21

How about not using recursion when calculating fibonacci numbers?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List