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