2011-01-23 13 views
23

¿Hay alguna diferencia entre utilizar mach_absolute_time y el método simple NSDate explicado a continuación por golden eagle?¿Código para medir el tiempo de ejecución EXACTA dentro del código en iPad o teléfono?

Aquí es una excelente explicación de utilizar el enfoque mach ...

How do I accurately time how long it takes to call a function on the iPhone?

y

Measure time between library call and callback

+0

posible duplicado de [¿Cómo puedo cronometrar con precisión el tiempo que lleva invocar una función en el iPhone?] (http://stackoverflow.com/questions/646815/how-do-i-accurately-time-how-long-it- takes-to-call-a-function-on-the-iphone) –

Respuesta

76
loop 
    { 
    NSDate *start = [NSDate date]; 

    // a considerable amount of difficult processing here 
    // a considerable amount of difficult processing here 
    // a considerable amount of difficult processing here 

    NSDate *methodFinish = [NSDate date]; 
    NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:start]; 

    NSLog(@"Execution Time: %f", executionTime); 
    } 

debería funcionar.

+0

¿La respuesta resolvió tu problema? –

+11

De acuerdo, desarrolladores de iPhone, todos toman un descanso para tomar café. – Will

2

Al answear anteriores he implementado una clase simple para medir el tiempo

Cómo funciona:

ABTimeCounter *timer = [ABTimeCounter new]; 
[timer restart]; 

//do some calculations 

[timer pause]; 

//do some other staff 

[timer resume]; 

//other code 

//You can measure current time immediately 

NSLog(@"Time left from starting calculations: %f seconds",[timer measuredTime]); 

[timer pause]; 

su archivo .h debería tener este aspecto:

@interface ABTimeCounter : NSObject 
@property (nonatomic, readonly) NSTimeInterval measuredTime; 

- (void)restart; 
- (void)pause; 
- (void)resume; 

@end 

archivo .m :

@interface ABTimeCounter() 
@property (nonatomic, strong) NSDate *lastStartDate; 
@property (nonatomic) BOOL isCounting; 
@property (nonatomic, readwrite) NSTimeInterval accumulatedTime; 
@end 

@implementation ABTimeMeasure 

#pragma mark properties overload 

- (NSTimeInterval) measuredTime 
{ 
    return self.accumulatedTime + [self p_timeSinceLastStart]; 
} 

#pragma mark - public - 

- (void) restart 
{ 
    self.accumulatedTime = 0; 
    self.lastStartDate = [NSDate date]; 
    self.isCounting = YES; 
} 

- (void) pause 
{ 
    if (self.isCounting){ 
     self.accumulatedTime += [self p_timeSinceLastStart]; 
     self.lastStartDate = nil; 
     self.isCounting = NO; 
    } 
} 

- (void) resume 
{ 
    if (!self.isCounting){ 
     self.lastStartDate = [NSDate date]; 
     self.isCounting = YES; 
    } 
} 

#pragma mark - private - 

- (NSTimeInterval) p_timeSinceLastStart 
{ 
    if (self.isCounting){ 
     return [[NSDate date] timeIntervalSinceDate:self.lastStartDate]; 
    } 
    else return 0; 
} 

@end 
+0

No reinventar la rueda. Downvoted. – Maq

+0

@Maq, ¿podría proporcionarnos más detalles para su comentario? –

Cuestiones relacionadas