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 );}