Подтвердить что ты не робот

Код для измерения времени выполнения EXACT внутри кода на iPad или телефоне?

Есть ли разница между использованием mach_absolute_time и простым NSDate методом, объясненным золотым орлом ниже?

Здесь отличное объяснение использования махового подхода...

Как точно указать время, необходимое для вызова функции на iPhone?

и

Измерение времени между вызовом библиотеки и обратным вызовом

4b9b3361

Ответ 1

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

Должен работать.

Ответ 2

При предыдущем ответе я применил простой класс для измерения времени

Как это работает:

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

ваш .h файл должен выглядеть так:

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

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

@end

.m file:

@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