I would like to know how long it's taking to send a message to an object with at least a 1ms accuracy. How do I do this?
views:
141answers:
2
+4
A:
Here's what I do:
NSDate * start = [NSDate date];
// do whatever I need to time
NSLog(@"time took: %f", -[start timeIntervalSinceNow]);
The output will be in seconds (with a decimal portion). NSDate
s have resolution on the scale of milliseconds, although I'm not sure precisely how accurate they are.
If the code you're trying to time is too fast, put it in a loop that runs the code a hundred times. (That assumes, of course, that the code you're timing has no side effects.)
Brent Royal-Gordon
2009-03-14 22:14:53
+5
A:
You can use mach_absolute_time
to measure in nanoseconds.
#import <mach/mach_time.h>
uint64_t startTime = 0;
uint64_t endTime = 0;
uint64_t elapsedTime = 0;
uint64_t elapsedTimeNano = 0;
mach_timebase_info_data_t timeBaseInfo;
mach_timebase_info(&timeBaseInfo);
startTime = mach_absolute_time();
//do something here
endTime = mach_absolute_time();
elapsedTime = endTime - startTime;
elapsedTimeNano = elapsedTime * timeBaseInfo.numer / timeBaseInfo.denom;
digdog
2009-03-15 02:16:44