iOS devices have limited memory and processing power relative to the computer, so a piece of code or program run time needs to be noticed, and here are two ways to get accurate time.
Method One: Use System time
1 nsdate* tmpstartdata = [[NSDate Date] retain]; 2 // You code here ... 3 double deltatime = [[nsdate Date] timeintervalsincedate:tmpstartdata]; 4 NSLog (@ ">>>>>>>>>>cost time =%f ms", deltatime*);
Method Two:
1 #import<mach/mach_time.h>2 3uint64_t start =mach_absolute_time ();4 //You code here ...5uint64_t end =mach_absolute_time ();6uint64_t elapsed = end-start;mach_timebase_info_data_t info;7 if(Mach_timebase_info (&info)! =kern_success)8 { 9printf ("Mach_timebase_info failed\n");Ten } Oneuint64_t nanosecs = elapsed * Info.numer/Info.denom; Auint64_t millisecs = nanosecs/1000000; -NSLog (@">>>>>>>>>>cost time =%ld ms", millisecs);
iOS test program runs time-consuming