Vala Programming Language Recursive Benchmark using Fibonacci
- June 16th, 2010
- Posted in Benchmarking . Programming . Vala
- By Dean Harris
- Write comment
In a previous post I took a look at benchmarking the performance of various programming languages using recursive Fibonacci. Today I decided to take a look at Vala.
Vala is a new programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C.
First, of course, I had to install Vala and a suitable IDE. To do this on Ubuntu 10.04, I followed the instructions found here.
Here is the code and the benchmark result:
public class Main
{
public static int Fibonacci(int n)
{
if (n < 2)
{
return n;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
public static int main (string[] args)
{
var timer = new Timer();
timer.start();
for (int i = 0; i < 36; i++)
{
stdout.printf("n=%d => %d\n", i, Fibonacci(i));
}
timer.stop();
stdout.printf("Time elapsed = %f seconds\n", timer.elapsed());
return 0;
}
}
Results: Time elapsed = 0.640000 seconds
As you can see, compared to the recursive python benchmark (Time elapsed = 14.2846501707 seconds), this is very fast.
Vala looks like a very interesting language and is one I’ll be watching the progress of closely.
Please note: I’m not a C coder and have never programmed in either C or Vala before, as such, my timing routine is probably poorly implemented at best! If anyone knows how I should have done this, I’d love to hear from you.
Many thanks to frederik (see comments below) for showing me a better way of determining elapsed time!

Hi,
GLib has a Timer class for benchmarking:
var timer = new Timer ();
timer.start ();
// …
timer.stop ();
stdout.printf (“Time elapsed = %f seconds\n”, timer.elapsed ());
Btw, you do not have to set up a class just for static methods (however, should not make a performance difference in this case). This is sufficient:
int fibonacci (int n) {
return n < 2 ? n : fibonacci (n – 1) + fibonacci (n – 2);
}
void main () {
var timer = new Timer ();
timer.start ();
for (int i = 0; i %d\n”, i, fibonacci (i));
}
timer.stop ();
stdout.printf (“Time elapsed = %f seconds\n”, timer.elapsed ());
}
Sorry, the blog software has broken the code, but you get the idea.