[iOS/OC] dispatch_apply性能分析

今天对dispatch_apply的性能进行了简单的分析,简单记录下。
dispatch_apply是GCD提供的,可以将迭代器转变为并发任务。
假设我们有一个计算斐波那契数列的算法(这里特意使用了递归):

long fibonacci(long x)
{
    if (x <=1) {
        return 1;
    } else {
        return fibonacci(x-1) + fibonacci(x-2);
    }
}

我们有一个事情要做:计算第15个斐波那契数:

- (void) dosomething {
    fibonacci(15);
}

我们有一个任务要做:做50次的重复计算dosomething(),通常我们的做法是:

for (int i = 0; i<count; i++) {
    [self dosomething];
}

这个方法是串行的,会依次执行每次的单个计算,直至结束。如果我们需要并行计算,有一个比较简单的方法,就是使用dispatch_apply:

dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_apply(count, queue, ^(size_t index){
    [self dosomething];
});

那么,这里就会有一个利弊权衡了。什么时候用for循环进行串行操作,什么时候用dispatch_apply进行并行操作。当每次任务都是独立的时候,两种方案都可以用,这里我从性能的角度分析下两个方案。
dispatch_apply是一种多线程方案,将需要进行线程切换,线程同步等操作,会有一定的耗时,当单次任务执行的耗时较短,甚至小于线程切换的耗时时,显然使用for循环进行串行操作性能更优。


- (void)touch {
    NSInteger count = 50;
    // 1.dispatch
    NSTimeInterval date00,date01;
    date00 = [[NSDate date] timeIntervalSince1970];
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_apply(count, queue, ^(size_t index){
        [self dosomething];
    });
    date01 = [[NSDate date] timeIntervalSince1970];

    // 2.for
    NSTimeInterval date10,date11;
    date10 = [[NSDate date] timeIntervalSince1970];
    for (int i = 0; i<count; i++) {
        [self dosomething];
    }
    date11 = [[NSDate date] timeIntervalSince1970];

    printf("dispatch\t: %f\n", date01 - date00);
    printf("for\t\t\t: %f\n", date11 - date10);
    printf("------------------------------------------------------------------\n");
}

此处,我使用时间戳对两个方案的耗时进行了统计。

设备:真机iTouch 6G
环境:singleView任务
结论:单次任务耗时10us为临界值;当单次任务耗时在10us以内时,串行操作性能更优,单次任务耗时在10us以上时,并行操作性能更优。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Started GET "/notebooks/" for 127.0.0.1 at 2023-07-14 09:59:56 +0800 Processing by NotebooksController#index as HTML Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms) NoMethodError (undefined method `all' for Notebook:Module): app/controllers/notebooks_controller.rb:4:in `index' Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.5ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (9.8ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (8.4ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (15.1ms)
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值