using GLib;

static bool use_library (string str)
{
	try {
		var re = new Regex ("""(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)""");
		MatchInfo info;

		if (re.match (str, 0, out info)) {
			return true;
		} else {
			return false;
		}
	} catch (RegexError err) {
		message (err.message);
	}
	return false;
}

static bool use_native_literals (string str)
{
	MatchInfo info;
	if (/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/.match (str, 0, out info)) {
		return true;
	} else {
		return false;
	}
}

static void main ()
{
	string[] data = {
		"2010-03-20 12:08 root backup-event success",
		"2010-03-20 12:18 dbuser pull-db failed",
		"2010-03-20 12:21 dbuser pull-db success",
		"2010-03-20 13:00 dbuser push-db success",
		"2010-03-20 13:08 root backup-event success"
	};

	TimeVal ts = TimeVal ();
	TimeVal te = TimeVal ();
	int iters = 100000;

	/* Using library methods. */
	ts.get_current_time ();
	for (int n=0; n<iters; n++) {
		for (int i=0; i<5; i++) {
			use_library (data[i]);
		}
	}
	te.get_current_time ();

	double elapsed = (double) te.tv_sec - ts.tv_sec;
	elapsed += (te.tv_usec - ts.tv_usec) / 1000000.0;
	int per_sec = (int) (iters*5 / elapsed);
	stdout.printf ("library:\t%g sec => %d per sec\n", elapsed, per_sec);

	/* Using regex literals. */
	ts.get_current_time ();
	for (int n=0; n<iters; n++) {
		for (int i=0; i<5; i++) {
			use_native_literals (data[i]);
		}
	}
	te.get_current_time ();

	elapsed = (double) te.tv_sec - ts.tv_sec;
	elapsed += (te.tv_usec - ts.tv_usec) / 1000000.0;
	int per_sec_n = (int) (iters*5 / elapsed);
	stdout.printf ("native: \t%g sec => %d per sec (%+.1f%%)\n", elapsed, per_sec_n, 100.0*((double) per_sec_n-per_sec)/per_sec);
}

