• GCD Timer事件的精度


    一、测试环境

      iPhoneX 真机+Debug模式,Timer代码工作在主线程,主线程空闲不阻塞

      在子线程统计每3秒tick计数,逐步减小inteval,看能达到多大精度。

      忽略原子计数值操作的影响 

    二、测试代码

      

    @interface ViewController ()
    
    @property (nonatomic, strong)   dispatch_source_t timer;
    @property (atomic, assign)   NSInteger   count;
    @property (nonatomic, strong)   NSDate *startDate;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self testTimer];
        [self dumpResult];
    }
    
    - (void)setupTimer
    {
        _startDate = [NSDate date];
        
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
        
        dispatch_source_set_timer(_timer,
                                  dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
                                  10 * NSEC_PER_USEC,
                                  0);
        
        dispatch_source_set_event_handler(_timer, ^{
            self.count++;
        });
        dispatch_resume(_timer);
    }
    
    - (void)dumpResult
    {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            while (YES) {
                [NSThread sleepForTimeInterval:3];
                NSLog(@"%lf per second", self->_count * 1.0 / [[NSDate date] timeIntervalSinceDate:_startDate] );
                self.count = 0;
                _startDate = [NSDate date];
            }
        });
    }
    
    - (void)testTimer
    {
        [self setupTimer];
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    

      

      间隔分别设置为1000us,100us,10us,对应结果如下;

      

    2018-08-21 20:18:59.344851+0800 TestGCDTimer[17447:7933588] 664.509536 per second
    2018-08-21 20:19:02.345519+0800 TestGCDTimer[17447:7933588] 998.187963 per second
    2018-08-21 20:19:05.347805+0800 TestGCDTimer[17447:7933588] 999.964033 per second
    2018-08-21 20:19:08.353024+0800 TestGCDTimer[17447:7933588] 999.657229 per second
    2018-08-21 20:19:11.354784+0800 TestGCDTimer[17447:7933588] 999.810779 per second
    2018-08-21 20:19:14.355783+0800 TestGCDTimer[17447:7933588] 999.763704 per second
    2018-08-21 20:19:17.359697+0800 TestGCDTimer[17447:7933588] 1000.090226 per second
    2018-08-21 20:19:20.364868+0800 TestGCDTimer[17447:7933588] 999.988378 per second
    2018-08-21 20:19:23.370107+0800 TestGCDTimer[17447:7933588] 999.985720 per second
    2018-08-21 20:19:26.374239+0800 TestGCDTimer[17447:7933588] 1000.022002 per second
    2018-08-21 20:19:29.375823+0800 TestGCDTimer[17447:7933588] 999.875706 per second
    
    
    2018-08-21 20:19:47.485125+0800 TestGCDTimer[17450:7934123] 6615.815753 per second
    2018-08-21 20:19:50.486199+0800 TestGCDTimer[17450:7934123] 9991.399568 per second
    2018-08-21 20:19:53.490001+0800 TestGCDTimer[17450:7934123] 9994.962862 per second
    2018-08-21 20:19:56.490189+0800 TestGCDTimer[17450:7934123] 9996.189939 per second
    2018-08-21 20:19:59.490784+0800 TestGCDTimer[17450:7934123] 9994.127387 per second
    2018-08-21 20:20:02.492282+0800 TestGCDTimer[17450:7934123] 9988.322134 per second
    2018-08-21 20:20:05.495670+0800 TestGCDTimer[17450:7934123] 9997.735664 per second
    2018-08-21 20:20:08.495762+0800 TestGCDTimer[17450:7934123] 9993.233265 per second
    2018-08-21 20:20:11.496856+0800 TestGCDTimer[17450:7934123] 9994.565064 per second
    2018-08-21 20:20:14.498354+0800 TestGCDTimer[17450:7934123] 9997.541106 per second
    
    
    2018-08-21 20:20:56.694542+0800 TestGCDTimer[17454:7934900] 23341.699400 per second
    2018-08-21 20:20:59.696130+0800 TestGCDTimer[17454:7934900] 33664.868549 per second
    2018-08-21 20:21:02.697670+0800 TestGCDTimer[17454:7934900] 31380.877279 per second
    2018-08-21 20:21:05.699141+0800 TestGCDTimer[17454:7934900] 34535.414907 per second
    2018-08-21 20:21:08.700716+0800 TestGCDTimer[17454:7934900] 34488.215922 per second
    2018-08-21 20:21:11.702206+0800 TestGCDTimer[17454:7934900] 34440.995680 per second
    2018-08-21 20:21:14.703723+0800 TestGCDTimer[17454:7934900] 34449.645662 per second
    

      

    三、结论

      GCD Timer 最大精度 0.03ms左右。完全可以达到ms级别精度

  • 相关阅读:
    动态网页技术--JSP(5)
    动态网页技术--JSP(4)
    动态网页技术--JSP(3)
    动态网页技术--JSP(2)
    动态网页技术--JSP(1)
    动态网页技术--Servlet
    TomCat服务器搭建
    06_多线程
    05_进程间通信 IPC
    04_进程池
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/9513760.html
Copyright © 2020-2023  润新知