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:
Anonymous2011-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:
Anonymous2011-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
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".