Gorilla3D

Blogs of my work and thoughts

Subscribe to RSS feed

Posts tagged with "Vala"

Simple Microsecond Timers in Vala

Using TimeVal

There are a few ways to produce a time. Some include seconds, some include microseconds (μ). For me I'd rather use the built-in methods. TimeVal is apart of the GLib library. TimeVal, when initialized it will grab the current timestamp automatically. Respectively the results are able to yield microseconds as a hole number. For more information about TimeVal visit Valadoc's "TimeVal"

// Initialize the time
var start_time = new TimeVal ();
var time = start_time.tv_sec + (start_time.tv_usec * 0.000001);
// Do things here
...
// Figure out how long it took...
var end_time = new TimeVal ();
time = end_time.tv_sec + (end_time.tv_usec * 0.000001) - time;

// Print out our time
print ("Work took " + time.to_string () + " seconds");
// Or
printerr ("Work took %f seconds", time);
// Or
print (@"Work took $time seconds");


A Better Way with Timer

Timer is also built in and a lot more sensible when try to accomplish a stop watch. If you need to pause, continue and get the time between all of it then Timer is a much better choice. Timer is also apart of GLib. The output yield microseconds in a decimal format. For more information about Timer visit Valadoc's "Timer"

// Initialize the Timer
var time = new Timer();
time.start ();

// Do Work
...

time.stop ();
/ Print out our time
print ("Work took " + time.elapsed ().to_string () + " seconds");
// Or
printerr ("Work took %f seconds", time.elapsed ());