Subscribe to Code Rants (and Misc. Other Things)        RSS Feed

Vala and C++, a test of Speed

Icon Leave Comment
OK. I've recently become addicted to Vala, and though I like C++ the best, I do find Vala to be refreshing. Vala's syntax is just like that of Java and C#, but of course it's fundamentally different in that it doesn't use a VM, and is plainly compiled like C++. It also has all the features of C++ (pointers and destructors for example are missing in Java).

Vala also makes the claim to bring no overhead over C++. It even says it may be faster. I decided to put it to the test. The challenge: simply iterate to 1,000,000 a bunch of times and just count CPU clock ticks :D I made all variables ahead of time to try and get the exact number :)

First Off, here's the code I used:
using GLib;

class speed : GLib.Object {
	public static int main() {
		stdout.printf("This program is to test whether C++ or Vala is faster.\n This is the Vala version. Run it, and please record times given.");
		var timer = new Timer();
		for(int i = 0; i <= 1000000; i++) {
			stdout.printf("%d\n", i);

		stdout.printf("That took %g seconds\n", timer.elapsed());
		return 0;

#include <cstdio>
#include <ctime>

int main() {
		printf("This program is to test whether C++ or Vala is faster.\n This is the Vala version. Run it, and please record times given. Press any key to begin...");
		int time1 = clock(), time2 = 0;
		for(int i = 0; i <= 1000000; i++) {
			printf("%d\n", i);

		time2 = clock();

		int result = time2-time1;
		printf("That took %d clock ticks\n", result);
		return 0;

Here's the results:
- 95.8778
- 50.9879
Average: 80.8053

- 98.904
- 96.049
- 98.202
Average: 97.7183

At first these results blew me away. I had placed my bets on C++, and not a single test was faster than Vala. What really got me was the Vala test that completed in 50 seconds!

In conclusion, Vala was definitely king, but for now I'll stick to C++. I mean getting worked up about 10 seconds here and there is kinda silly (most applications won't be counting to a million either :P). I'm better and more experienced with C++, and Vala is mostly a toy for me to play with. However, I could really see it being applied to Science and Mathematics, in my future.

~ Mike

P.S. I did use different compilers. The Vala app was built under MinGW, and the C++ one in VC++. This might have has some affect on it, but I doubt much. I'll rebuild them later using the same compiler, and if there are any major improvements, I'll repost to let ya know :D

0 Comments On This Entry


March 2019

17 18 1920212223

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)