Friday, August 14, 2009

A Small Mathematica Vs C Benchmark

Sometimes I get frowned upon because I do expensive computations in Mathematica. Some expensive computations will be very fast in Mathematica, of course. Try computing (10^4)//Factorial.

But what about the cheap computations that you could as well do in C? The Euler Project gave me the change to try it out with an easy problem. I was solving problem 9 from project Euler, which in short searches for natural numbers a<b<c, such that a^2+b^2=c^2, and a+b+c=1000.

The C-program that finds these numbers is


int main(void) {
for(int a = 1; a < 10000; a++) {
for(int b = a + 1; b <= 10000; b++) {
int c = 1000 - a - b;
if(a * a + b * b == c * c) {
printf("%d\n",a*b*c);
return 0;
}
}
}
}


Note that I de-optimized a little bit by bounding a and b in [1,10,000], which is ten times greater than it should be. The result is still correct, but now it's slow enough so that we can measure something.

The run-time in C is 0.016 seconds on my machine

Bringing this to Mathematica is more than straightforward:


Module[{a, b, c},
For[a = 1, a <= 10000, a++,
For[b = a + 1, b <= 10000, b++,
c = 1000 - a - b;
If[a a + b b == c c, Return[a b c]]
]
];
0
][]



Now the run-time is at 8.5 seconds. But! Mathematica can compile! To something faster, that is. It's as easy as wrapping a Compile around the computation. We get:


Compile[{},
Module[{a, b, c},
For[a = 1, a <= 10000, a++,
For[b = a + 1, b <= 10000, b++,
c = 1000 - a - b;
If[a a + b b == c c, Return[a b c]]
]
];
0
]
]



And now we're down to 0.38 seconds. Just as a reminder:

Table 1. Run times of the Euler project 9 algorithm in seconds. M stands for Mathematica
C uncompiled M compiled M
0.16 8.53 0.38



Which brings me to the following conclusion: For basic arithmetic computation, Mathematica will slow you down by a factor 40, if you use it only casually, or by a factor of 2 if you have some experience with it (Not everything is nicely compilable, so this is a bit optimistic).

Mathematica taketh away, but it also giveth: The library functions are fast, and there's plenty of them. You can treat Mathematica just like any scripting language: If the operations in your inner loop are costly, Mathematica is good for you! Otherwise, you pay a price for a wide range of library-functions. And it is worth delving into that library.