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