Acquisition of precise time in iOS

Source: Internet
Author: User

Here is an impossible to verify the source of English and their own translation

A quick and easy-to-measure the performance of a piece of IOS code is to dig down below all the Cocoa Touch stuff and Use the low-level mach_absolute_time . This call returns a ticks count that can is converted into nanoseconds using information obtained from the call mach_timebase_info . The following code sketches out the technique:

An easy and quick way to accurately measure the performance of an iOS code is to dig deep into the layers below the cocoa touch layer-using the underlying call Mach_absolute_time. The Mach_absolute_time returns the tick count counter of the CPU (which is considered a CPU time privately), and the returned time can be converted to a nanosecond level. The following is a demonstration of the implementation of the underlying call mach_absolute_time.

Add (Attached)

#import <mach/mach_time.h>//Import the underlying framework to invoke

Double Machtimetosecs (uint64_t time)//cpu TickCount conversion into a function

{

mach_timebase_info_data_t timebase;

Mach_timebase_info(&timebase);

return (Double) time * (Double) timebase. Numer/

(double) timebase. denom/1e9;

}

-(void) profiledosomething// method of detecting code execution time

{

uint64_t begin = Mach_absolute_time(); Get time with Mach_absolute_time()

Here's the iOS code to test,

For (int i = 0; i <; i++) {

NSLog (@ "Test");

}

uint64_t end = Mach_absolute_time(); Get time with Mach_absolute_time()

NSLog(@ "Time taken to dosomething%g S",

machtimetosecs (End-begin)); End-begin and then convert, you get the exact execution time.

}

The timebase values on the simulator, in least on my MacBook Air, is 1/1, however on the IPad and IPhone 4 they is 125/3 ; So don ' t is lazy and hard code them.

(This paragraph in English mainly talk about different test equipment to get different results, because the landlord do not know 1/1,125/3 what meaning so this paragraph is not translated)

Never assume that because something runs quickly on the simulator it would also run quickly on a target device. On II jobs last year I encountered code this took 100x longer to run a iPad than in the simulator.

Never assume that the code performs as efficiently on the emulator as it does on the target device (such as the iphone 5). I met a piece of code last year that ran 100X more on the ipad than on the simulator.

Acquisition of precise time in iOS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.