国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用dispatch_benchmark函數進行基準測試

2019-11-14 19:10:21
字體:
來源:轉載
供稿:網友

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

輸出:

[[NSMutableArray array] addObject:] Avg. Runtime: 35960141 ns[[NSMutableArray arrayWithCapacity:] addObject:] Avg. Runtime: 32219393 ns

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 介休市| 深水埗区| 资源县| 郎溪县| 吴堡县| 巫溪县| 南昌市| 安康市| 依兰县| 河北省| 福清市| 友谊县| 航空| 娄烦县| 东平县| 娱乐| 广东省| 墨玉县| 彭山县| 永修县| 塘沽区| 莱芜市| 韩城市| 新巴尔虎右旗| 安顺市| 洪泽县| 沂南县| 普格县| 湾仔区| 涞源县| 景德镇市| 桃源县| 杂多县| 宝丰县| 五指山市| 邵阳县| 越西县| 靖安县| 宾阳县| 安远县| 达州市|