Showing posts with label rpython. Show all posts
Showing posts with label rpython. Show all posts

Saturday, February 23, 2008

Compiling Python through Lisp?













Several articles have described the similarities between Python and Lisp, e.g.
From my perspective Lisp compilers seems mature and produce relatively high performance code (about 10 times faster than Python), and if Python is compiled to Lisp it can probably be done without any restrictions to how the Python code is written (which doesn't seem to be the case with Shedskin and RPython that compile to C/C++ code).

A possible approach could be to to use the Antlr Python 2.5 grammar and rewrite to a tree grammar (which looks very similar to Lisp), and with the forthcoming Antlr version you can rewrite the tree grammars to another tree grammar which happen to be Lisp?

It is probably not as simple as suggested, but let me know if you try :-)

Tuesday, February 19, 2008

Greenlet Python is concurrently alive and kicking


As I mentioned before I am a big fan of the Python programming language, and for good reasons, in particular support for thousands of simultaneous lightweight threads (tasklets) with Stackless Python (which requires a modified Python interpreter).

Greenlet and Eventlet
What I recently discovered was Greenlets. It is a spinn-off library from Stackless Python but as opposed to Stackless it is supported by the standard Python interpreter. There are also some interesting additional libraries based on Greenlets, e.g. the Eventlet networking library.

(Hm, maybe using Greenlets with Parallel Python could be a thought)

Concurrency seems to be getting increasingly more attention, and it is great to see that Python is not falling behind, actually far from it. Maybe Python can be used to solve some of the challenges in concurrency.

Tuesday, January 29, 2008

Stackless RPython - Recursive

Got input from pypy/rpython developers, and in order to get stackless rpython it was just to add parameter stackless=True and replace parameter gc='ref' with gc='generation' to Translation() method.
# The Computer Language Shootout
# http://shootout.alioth.debian.org/
# based on bearophile's psyco program
# slightly modified by Isaac Gouy
# And adapted to RPython by Amund

def Ack(x, y):
if x == 0: return y+1
if y == 0: return Ack(x-1, 1)
return Ack(x-1, Ack(x, y-1))

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

def FibFP(n):
if n < 2.0: return 1.0
return FibFP(n-2.0) + FibFP(n-1.0)

def Tak(x, y, z):
if y < x:
return Tak(
Tak(x-1,y,z),
Tak(y-1,z,x),
Tak(z-1,x,y) )
return z

def TakFP(x, y, z):
if y < x:
return TakFP(
TakFP(x-1.0,y,z),
TakFP(y-1.0,z,x),
TakFP(z-1.0,x,y) )
return z

# RPython stuff starts here

from sys import argv #, setrecursionlimit
#setrecursionlimit(12345678901)

def main(argv):
n = int(argv[1]) - 1
print "Ack(3,%d):" % (n+1), Ack(3, n+1)
print "Fib(" + str(28.0+n) + "," + str(FibFP(28.0+n))
print "Tak(%d,%d,%d): %d" % (3*n, 2*n, n, Tak(3*n, 2*n, n))
print "Fib(3):", Fib(3)
print "Tak(3.0,2.0,1.0):", TakFP(3.0, 2.0, 1.0)
return 0

from pypy.translator.interactive import Translation
#t = Translation(main, standalone=True, gc='ref')
t = Translation(main, standalone=True,
stackless=True, gc='generation')
t.source(backend='c')
path = t.compile()
print path

Monday, January 28, 2008

RPython GCLB Benchmark - Recursive

Must admit that I am a big fan of python (the programming language), and when I saw the benchmark that RPython can be faster than C (on the binary tree benchmark from the Great Computer Language Shootout - GCLS), I just had to try RPython on another problem from GCLS, so I chose the one with worst performance compared to C/gcc (266 times slower) - recursive (with various recursive methods, e.g. Ackerman, Fibonacci and Tak). Results were roughly that rpython was 50-100 and gcc/c was 100-300 times faster than python (note: I only did one run, so numbers can be somewhat bogus, but not too bad I think).



Unfortunately for the run with n=11 Ackerman had consumed all the stack, but that can be solved with Stackless RPython.