Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

iPhone: High-resolution time?

What is the best way to get high-resolution time-stamps (e.g., microsecond) on the iPhone? The API calls I've been able to find return time in seconds (time_t or CFTimeInterval).

Thanks!

-Chris

iPhone, Mac OS X (10.5.4), iPhone Developer

Posted on Jul 26, 2008 12:15 PM

Reply
2 replies

Aug 5, 2008 4:43 PM in response to rockmuelle

I've found CFAbsoluteTimeGetCurrent() to be extremely unreliable. In particular, sometimes it "freezes" and continually returns the same value over a lengthy span of time. It can also be disrupted on cell carrier signal loss, 2G/3G/roam switch, etc.

Although you don't have access to Core Services on iPhone, you do have access to some of the basic Mach timing functions, and I've found the following simple class to be extremely useful. Just create an instance, call [timer start] to reset to zero, and use [timer elapsedSec] to return the elapsed time since the call to start with float precision.

//
// MachTimer.h
//
#include <assert.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>
@interface MachTimer : NSObject {
uint64_t t0;
}
- (void)start;
- (uint64_t)elapsed;
- (float)elapsedSec;
@end
//
// MachTimer.m
//
#import "MachTimer.h"
static machtimebase_info_datat timebase;
@implementation MachTimer
+ (void)initialize
{
(void) machtimebaseinfo(&timebase);
}
- init
{
if(self = [super init]) {
t0 = machabsolutetime();
}
return self;
}
- (void)start
{
t0 = machabsolutetime();
}
- (uint64_t)elapsed {
return machabsolutetime() - t0;
}
- (float)elapsedSec {
return ((float)(machabsolutetime() - t0)) * ((float)timebase.numer) / ((float)timebase.denom) / 1000000000.0f;
}
@end


Message was edited by: Crashing00

iPhone: High-resolution time?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.