View the code running time on iOS, and view the code on ios

Source: Internet
Author: User

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:

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.