Use Objective-C to calculate the code running time and objective-c code

Source: Internet
Author: User

Use Objective-C to calculate the code running time and objective-c code
First: (the simplest NSDate)

NSDate* tmpStartData = [NSDate date];//You code here...double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData];NSLog(@"cost time = %f", deltaTime);
Type 2: (put the Running code in the following Block and return the time)
#import <mach/mach_time.h>  // for mach_absolute_time() and friends    CGFloat BNRTimeBlock (void (^block)(void)) {      mach_timebase_info_data_t info;      if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;        uint64_t start = mach_absolute_time ();      block ();      uint64_t end = mach_absolute_time ();      uint64_t elapsed = end - start;        uint64_t nanos = elapsed * info.numer / info.denom;      return (CGFloat)nanos / NSEC_PER_SEC;  }
Third:
/*** Calculate the script time * @ param $ last clock * @ param $ key ID * @ return Current clock */double t (double last, char * key) {clock_t now = clock (); printf ("time: % fs \ t key: % s \ n", (last! = 0 )? (Double) (now-last)/CLOCKS_PER_SEC: 0, key); return now;} double t1 = t (0, ""); // do somethingt (t1, "end ");



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.