View the code running time on iOS, and view the code on ios
Sometimes we want to know exactly the time for executing a code segment or loop, and then analyze the efficiency and other issues. What is the execution time required at this time. I just saw someone doing this on the internet, and I just picked it up. The macro method is used to calculate the time. We only need to write TICK and TOCK macros before and after the code block that requires time calculation. Of course, the principle is also very simple, that is, using NSDate to calculate the difference value.
1234567891011121314151617181920212223 |
#import "ViewController.h" #define TICK NSDate *startTime = [NSDate date] #define TOCK NSLog(@"Time: %f", -[startTime timeIntervalSinceNow]) @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [ super viewDidLoad]; TICK; for (int i = 0; i < 5; i++) { NSLog(@ "My value: % d" ,i); } TOCK; } @end |
The output is as follows: