Use the dispatch_benchmark function for benchmarking. the benchmark Function

Source: Internet
Author: User

Use the dispatch_benchmark function for benchmarking. the benchmark Function

The dispatch_benchmark function is part of libdispatch (Grand Central Dispatch). This method is not publicly declared, so you must declare it yourself.

 

Introduction

The dispatch_benchmark function executes the given block multiple times according to the count variable and then returns the average number of nanoseconds per execution. This function is for debugging and performance analysis work. For the best results, pass a high count value to dispatch_benchmark.

 

Function prototype

extern uint64_t dispatch_benchmark(size_t count, void (^block)(void))

 

Instance code

# Import <Foundation/Foundation. h> // declare the dispatch_benchmark function prototype extern uint64_t dispatch_benchmark (size_t count, void (^ block) (void); void test1 (void); void test2 (void ); int main (int argc, const char * argv []) {@ autoreleasepool {test1 (); test2 ();} return 0;} void test1 (void) {// number of executions size_t count = 1000; // call the dispatch_benchmark function to pass in the number of executions and the code block to be tested NSUInteger length = 1000000; uint64_t time = dispatch_benchmark (count, ^ {@ autoreleasepool {NSMutableArray * mutableArray = [NSMutableArray array]; for (NSUInteger I = 0; I <length; I ++) {[mutableArray addObject: @ (I)] ;}}); NSLog (@ "[NSMutableArray array] addObject:] Avg. runtime: % llu ns ", time);} void test2 (void) {// number of executions size_t count = 1000; NSUInteger length = 1000000; // call the dispatch_benchmark function, passed in execution times and code block to be tested uint64_t time = dispatch_benchmark (count, ^ {@ autoreleasepool {NSMutableArray * mutableArray = [NSMutableArray Duration: length; I <length; I ++) {[mutableArray addObject: @ (I)] ;}}); NSLog (@ "[[NSMutableArray arrayWithCapacity:] addObject:] Avg. runtime: % llu ns ", time );}

 

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.