使用dispatch_benchmark函數進行基準測試,benchmark函數

來源:互聯網
上載者:User

使用dispatch_benchmark函數進行基準測試,benchmark函數

dispatch_benchmark函數是libdispatch (Grand Central Dispatch) 的一部分,這個方法並沒有被公開聲明,所以必須要自己聲明。

 

簡介

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.

 

函數原型

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

 

執行個體代碼

#import <Foundation/Foundation.h>    // 聲明GCD中dispatch_benchmark函數原型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) {    // 執行次數    size_t count = 1000;    // 調用dispatch_benchmark函數,傳入執行次數和待測試的代碼塊    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) {    // 執行次數    size_t count = 1000;    NSUInteger length = 1000000;    // 調用dispatch_benchmark函數,傳入執行次數和待測試的代碼塊    uint64_t time = dispatch_benchmark(count, ^{        @autoreleasepool {            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:length];            for (NSUInteger i = 0; i < length; i++) {                [mutableArray addObject:@(i)];            }        }    });    NSLog(@"[[NSMutableArray arrayWithCapacity:] addObject:] Avg. Runtime: %llu ns", time);}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.