Saturday, February 17, 2007

Timing operations

I'll sometimes want to measure the time a certain operation takes and compare it with another operation or to compare it with another implementation of the same operation. Here's the fragment of Cocoa code I use to do that:

NSDate *startTime = [NSDate date];

{ ... } // the operation that I'm timing

NSTimeInterval elapsedTime = -[startTime timeIntervalSinceNow];

NSLog(@"That took %f seconds", elapsedTime);

This code grabs the current time using NSDate, does something, calculates the time spent doing the operation, and then logs the result.

0 comments: