How to measure the time between two lines of code in Objective-C for iOS?

I have the following code that takes time to process:

self.itemsArray = [Helper changeTheA:self.itemsArray]; self.itemsArray = [Helper convertDates:self.itemsArray]; 

Is there a way, in the Tools or elsewhere that I can measure the time, I need to go from the first line of code to the second line of code .... in ticks or milliseconds or something like that?

I want to make some adjustments, but I need to be able to measure to see if I am improving the previous code.

Thanks.

+6
source share
3 answers

The fastest, dirtiest, perhaps not very accurate way.

 NSDate *startDate = [NSDate date]; self.itemsArray = [Helper changeTheA:self.itemsArray]; NSLog(@"changeTheA time taken: %f", -[startDate timeIntervalSinceNow]); 

A slightly more active, but probably more useful solution, if you are doing basic profiling, can be seen here , it uses the C function, which executes the block that you provide.


A note from the big side

Since Justin points to the actual act of creating an NSDate and then registering it will lead to interference with your measurements, and therefore this technique is really good for getting park data, and even then you should probably run large amounts of iterations of your code in the synchronization block. If you need accurate measurements, skip Justin's answer

+6
source

Yes. For this you must use the sampler tools.

Launch the application, run the program for a while, then find the symbol in the Tools that contains:

 self.itemsArray = [Helper changeTheA:self.itemsArray]; self.itemsArray = [Helper convertDates:self.itemsArray]; 

It should show weights in rows. Also note that the tools allow you to specify a sampling rate of up to 40 microseconds.

+6
source

I would use the following code:

 NSTimeInterval start = CACurrentMediaTime(); // your code goes here NSTimeInterval end = CACurrentMediaTime(); NSTimeInterval delta = end - start; NSLog(@"Execution took %f seconds.", delta); 

This is much more accurate than NSDate .

+2
source

Source: https://habr.com/ru/post/913582/


All Articles