2011-01-23 9 views
23

mach_absolute_time और सरल NSDate विधि का उपयोग सुनहरा ईगल द्वारा समझाया गया विधि के बीच कोई अंतर है?आईपैड या फोन पर कोड के अंदर निष्पादन निष्पादन समय को मापने के लिए कोड?

यहाँ मच दृष्टिकोण का उपयोग कर के एक उत्कृष्ट व्याख्या दी गई है ...

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

और

Measure time between library call and callback

+0

संभव का डुप्लिकेट [आईफोन पर फ़ंक्शन कॉल करने में मुझे कितना समय लगता है?] (http://stackoverflow.com/questions/646815/how-do-i-accurately-time-how-long-it- ले-टू-कॉल-ए-फ़ंक्शन-ऑन-द-आईफोन) –

उत्तर

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); 
    } 

काम करना चाहिए।

+0

क्या उत्तर आपकी समस्या का समाधान करता है? –

+11

ठीक है, आईफोन देवता है, हर कोई कॉफी ब्रेक लेता है। – Will

2

पिछले answear पर मैं समय को मापने के लिए

एक साधारण वर्ग को लागू किया है यह कैसे काम करता है:

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

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

@end 

मीटर फ़ाइल:

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]; 

अपने ज फ़ाइल इस तरह दिखना चाहिए :

@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

पहिया को पुन: पेश न करें। Downvoted। – Maq

+0

@ मॅक, क्या आप अपनी टिप्पणी के लिए अधिक जानकारी प्रदान कर सकते हैं? –

संबंधित मुद्दे