Tuesday, July 31, 2012

Matlab vs Go: Performance

I'll keep it brief.

I have some parallel CPU-bound code for financial analysis in Matlab, that doesn't make use of any special Matlab package and lags a bit in terms of performance. I decided it would be easy and fun to translate it into Go and see how well it would run.

In Matlab, I made use of the parfor construct that runs a given piece of code over the elements of an array, distributing the computation among a pool of workers, either local (as in my case) or remote. No such thing exists in Go, but it only took about 10 lines of code to implement a local parfor based on goroutines.

I tested the speedup of both codes going from 1 to 4 processes/goroutines (on a quad-core machine). It was nice to see some consistency in the numbers with Matlab showing a speedup of 2.58 and Go of 2.62.

I then compared pure execution times and, hear, hear, in both sequential and parallel scenarios I got an improvement of about 13.5 times from Matlab to Go.

Quite impressive. The code I used is pretty much just fixed/floating point arithmetic over large collections of data in memory. Other kinds of programs may yield different results, and of course if you're programming in Matlab you'll have the advantage of being able to use its vast collection of technical packages.

All in all, way to Go!

No comments: