Saturday, February 19, 2011

CPython, Pypy, Java, and Go

I recently gave a talk at NYC Python comparing the speeds of CPython, Pypy, and Java for the calculation of a recursive Fibonacci function. The gist of the talk was that to obtain the best performance, you need to think in the specific language.  In the standard Fibonacci recursive algorithm, Cpython and PyPy were much slower than Java.  When a memoizing decorator was added to the Python code the performance became almost constant.

The first version of this blog entry incorrectly stated that the idiomatic Go code was recursive.  Thanks to Andrew Gerrand, of the Go team at Google, for pointing out my error.

I've become a big fan of Go. If you don't know about it, check it out at Go Language.

Test Results

Here is the code:

//memo.py
//From Python Algorithms by Magnus Lie Heitland
from functools import wraps

def memo(func):
    cache = {}
    @wraps(func)
    def wrap(*args):
        if args not in cache:
            cache[args] = func(*args)
        return cache[args]
    return wrap


//fibonacci.py
from memo import memo
import sys

@memo # Comment this out for the first set of tests.
def fib(i):
    if i < 1:
        return 1
    return fib(i - 1) + fib(i - 2)

def main():
    n = int(sys.argv[1])
    print fib(n)

if __name__ == "__main__":
    main()

//fibonacci_recr.go
package main

import (
       "fmt"
       "os"
       "strconv"
)

func fib(n int64) int64 {
       if n <= 1 {
               return n
       }
       return fib(n-1) + fib(n-2)
}

func main() {
       n, _ := strconv.Atoi64(os.Args[1])
       for i := int64(1); i <= n; i++   {
               fmt.Println(fib(i))
       }
}


//Fibonacci.java
public class Fibonacci {
    public static long fib(int n) {
        if (n <= 1) return n;
        else return fib(n-1) +  fib(n-2);
    }

    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        for (int i = 1; i <= N; i  )
            System.out.println(i +  ": " + fib(i));
    }
}


//fibonacci.go
package main

import (
    "os"
    "strconv"
)

func fib() func() int64 {
    var a,b int64
    a, b = 0,1
    return func() int64 {
        a, b = b, a+b
        return a
    }
}

func main() {
    a := os.Args[1]
    N, err := strconv.Atoi(a)
    if err != nil {
        println(err)
        return
    }
    f := fib()
    for i := 0; i <= N; i   {
        println(f())
    }
}


Friday, February 11, 2011

Gmail 500 Error and Two Step Verification

I was a beta tester for Google's two step verification. About three weeks ago, I disabled it in Google Profile because it was causing problems with the Outlook calendar synchronization that I maintained at a client site. On February 10th they promoted the verification process to production.

Like most major changes to Google products, I assume that it was rolled out to chunks of users at a time. Apparently, my chunk came up yesterday afternoon. I went to check my email and received a 500 Temporary Error. I clicked the link at the bottom of the page for more details and it said "Code 58".

I searched and received the usual explanations for for a 500 error and the nothing of substance regarding Code 58. Figuring that it really was a temporary server error I tried at 5 minute intervals for 15 minutes and with no success. At that point, I checked the App Status Dashboard and it showed that Gmail was fine. I searched the Gmail Help Center and when I did not find any helpful information I submitted the details to Google via the contact link. I then entered the details on Twitter and received one response from someone who had experienced the same situation.

Since I had turned off the two step verification process weeks ago and it was not the launch day, I did not immediately think that verification might be causing the problem. When I went into the account section of my profile, it showed that two step verification was off, so I enabled it. When I attempted to log into Gmail I was not prompted for a verification code, I simply entered my password and went right into Gmail. I returned to my profile and two step verification was disabled

I'd like to hear from anyone else who has had similar problems.