Author: Mark Szymczyk
Last Update: October 5, 2005

This article shows you how to accurately measure time on Mac OS X using the Microseconds() and UpTime() function calls.


Every frame action games read the player’s input, move objects that are in motion to their new positions, and draw the objects in their new positions. How much should you move the objects each frame? Assuming you store each object’s velocity, you calculate how far to move each object by multiplying the velocity by the amount of time that elapsed since the previous frame. If an object’s velocity was 100 units per second, and .1 seconds elapsed, you would move the object 10 units. Keeping track of the elapsed time keeps the game’s physics independent from its frame rate on the player’s machine.

There are two functions to help you calculate the elapsed time on Mac OS X: Microseconds() and UpTime(). Carbon applications can use these functions without having to do anything special. Cocoa programs that want to use these functions must add the Core Services framework to their projects. To use Microseconds() in a Cocoa program, you must include the file Timer.h in your program.

#import <CoreServices/Timer.h>

To use UpTime() in a Cocoa program, you must include the file DriverServices.h.

#import <CoreServices/DriverServices.h>

Next (Microseconds)