Friday, June 5, 2009

Language Benchmarks and Psyco Python

Ever wonder about how fast certain programming or scripting languages are, versus one another? Well, I have...

The Premise

So, being that MacPorts has Psyco available to it, I've been playing around with it. For those of you not in the know, Psyco is a JIT compiler and optimizer for Python. I came across this page of benchmarks and thought it would be fun to play around with the various implementations and see how fast they got.

The Machine

For the tests, I'm using my MacBook Pro (2.8 GHz Intel Core 2 Duo w/6MB L2, 4 GB DDR3 @ 1 GHz). As for the software:

ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
This is perl, v5.8.9 built for darwin-2level
Python 2.6.2 (r262:71600, Jun  5 2009, 17:59:46)
    [GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Java(TM) SE Runtime Environment (build 1.6.0_07-b06-153)
    Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_07-b06-57, mixed mode)
gcc (GCC) 4.4.0

Additional parameters passed to GCC:

c-regular: gcc bench.c
c-nocona:  gcc -O3 -march=nocona -m64 bench.c
c-tuned:   gcc -O3 bench.c

The Code (with Psyco)

I modified all of the scripts to get rid of their built-in timing, preferring instead the time UNIX command (real). Also, I stopped them from actually printing the fractal, but kept in the various 'print' commands as some of the implementations require at least something in their if-else block.

#!/usr/bin/env python
# by Daniel Rosengren
# Modified by Sebastian Weigand

import sys, psyco
stdout = sys.stdout

psyco.full()

BAILOUT = 16
MAX_ITERATIONS = 1000

class Iterator:
  def __init__(self):
    for y in range(-39, 39):
      for x in range(-39, 39):
        i = self.mandelbrot(x/40.0, y/40.0)
        
        if i == 0:
          stdout.write('')
        else:
          stdout.write('')
    
  def mandelbrot(self, x, y):
    cr = y - 0.5
    ci = x
    zi = 0.0
    zr = 0.0
    i = 0

    while True:
      i += 1
      temp = zr * zi
      zr2 = zr * zr
      zi2 = zi * zi
      zr = zr2 - zi2 + cr
      zi = temp + temp + ci
     
      if zi2 + zr2 > BAILOUT:
        return i
      if i > MAX_ITERATIONS:
        return 0

Iterator()

You can easily download the other files here, and run the benchmarks for yourself.

The Results

Well, as you might expect, they are a tad bit faster than the PowerBook in question. I just took a few of the tests, and came out with these results:

Benchmark bars

It's amazing just how much faster Psyco can make Python!

Interesting too, how unless you've got some really high-end SSE3 or higher code, with lots of matrices and whatnot, GCC won't make your code that much faster if you have it match your current high-end CPU. This holds true for a lot of things actually (like Vorbis). See what I mean by adjusting the MAX ITERATIONS to something like 100,000 or so.

Any thoughts on Psyco?

0 comments :

Post a Comment