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