如何准确计算在iPhone上调用功能所需的时间?

时间:2021-05-28 04:02:42

I would like to know how long it's taking to send a message to an object with at least a 1ms accuracy. How do I do this?

我想知道将消息发送到对象的时间长度至少为1ms。我该怎么做呢?

3 个解决方案

#1


15  

You can use mach_absolute_time to measure in nanoseconds.

您可以使用mach_absolute_time以纳秒为单位进行测量。

#import <mach/mach_time.h>

uint64_t startTime = 0;
uint64_t endTime = 0;
uint64_t elapsedTime = 0;
uint64_t elapsedTimeNano = 0;

mach_timebase_info_data_t timeBaseInfo;
mach_timebase_info(&timeBaseInfo);

startTime = mach_absolute_time();

//do something here

endTime = mach_absolute_time();

elapsedTime = endTime - startTime;
elapsedTimeNano = elapsedTime * timeBaseInfo.numer / timeBaseInfo.denom;

Reference: Technical Q&A QA1398: Mach Absolute Time Units

参考:技术问答QA1398:马赫绝对时间单位

#2


12  

Here's what I do:

这是我做的:

NSDate * start = [NSDate date];
// do whatever I need to time
NSLog(@"time took: %f", -[start timeIntervalSinceNow]);

The output will be in seconds (with a decimal portion). NSDates have resolution on the scale of milliseconds, although I'm not sure precisely how accurate they are.

输出将以秒为单位(带小数部分)。 NSDates具有毫秒级的分辨率,尽管我不确定它们的准确度。

If the code you're trying to time is too fast, put it in a loop that runs the code a hundred times. (That assumes, of course, that the code you're timing has no side effects.)

如果您尝试计算的代码太快,请将其置于运行代码一百次的循环中。 (当然,这假设您正在计时的代码没有副作用。)

#3


0  

Use dispatch_benchmark to log method execution time in nanoseconds.
It has a much nicer syntax than manually looping and calling mach_absolute_time() dispatch_benchmark is part of Grand Central Dispatch. This function is not publicly declared, so you’ll have to do that yourself before use :

使用dispatch_benchmark以纳秒为单位记录方法执行时间。它具有比手动循环更好的语法,并且调用mach_absolute_time()dispatch_benchmark是Grand Central Dispatch的一部分。此功能未公开声明,因此您必须在使用前自行完成此操作:

extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));

extern uint64_t dispatch_benchmark(size_t count,void(^ block)(void));

I am using this for log execution time :

我正在使用它来执行日志:

size_t const  blockIterations = 1; //block call count
uint64_t t = dispatch_benchmark(blockIterations, ^{ //your code inside block
        [self TEST_processJSONDataRecordsWithCompletionHandler:^(id handler) {}];
    });
NSLog(@" Avg. Runtime in nanoseconds : %llu ns", t);

Credit goes to Benchmarking

归功于基准测试

#1


15  

You can use mach_absolute_time to measure in nanoseconds.

您可以使用mach_absolute_time以纳秒为单位进行测量。

#import <mach/mach_time.h>

uint64_t startTime = 0;
uint64_t endTime = 0;
uint64_t elapsedTime = 0;
uint64_t elapsedTimeNano = 0;

mach_timebase_info_data_t timeBaseInfo;
mach_timebase_info(&timeBaseInfo);

startTime = mach_absolute_time();

//do something here

endTime = mach_absolute_time();

elapsedTime = endTime - startTime;
elapsedTimeNano = elapsedTime * timeBaseInfo.numer / timeBaseInfo.denom;

Reference: Technical Q&A QA1398: Mach Absolute Time Units

参考:技术问答QA1398:马赫绝对时间单位

#2


12  

Here's what I do:

这是我做的:

NSDate * start = [NSDate date];
// do whatever I need to time
NSLog(@"time took: %f", -[start timeIntervalSinceNow]);

The output will be in seconds (with a decimal portion). NSDates have resolution on the scale of milliseconds, although I'm not sure precisely how accurate they are.

输出将以秒为单位(带小数部分)。 NSDates具有毫秒级的分辨率,尽管我不确定它们的准确度。

If the code you're trying to time is too fast, put it in a loop that runs the code a hundred times. (That assumes, of course, that the code you're timing has no side effects.)

如果您尝试计算的代码太快,请将其置于运行代码一百次的循环中。 (当然,这假设您正在计时的代码没有副作用。)

#3


0  

Use dispatch_benchmark to log method execution time in nanoseconds.
It has a much nicer syntax than manually looping and calling mach_absolute_time() dispatch_benchmark is part of Grand Central Dispatch. This function is not publicly declared, so you’ll have to do that yourself before use :

使用dispatch_benchmark以纳秒为单位记录方法执行时间。它具有比手动循环更好的语法,并且调用mach_absolute_time()dispatch_benchmark是Grand Central Dispatch的一部分。此功能未公开声明,因此您必须在使用前自行完成此操作:

extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));

extern uint64_t dispatch_benchmark(size_t count,void(^ block)(void));

I am using this for log execution time :

我正在使用它来执行日志:

size_t const  blockIterations = 1; //block call count
uint64_t t = dispatch_benchmark(blockIterations, ^{ //your code inside block
        [self TEST_processJSONDataRecordsWithCompletionHandler:^(id handler) {}];
    });
NSLog(@" Avg. Runtime in nanoseconds : %llu ns", t);

Credit goes to Benchmarking

归功于基准测试