CFAbsoluteTimeGetCurrent 时间间隔的快速计算

1、效果


2、看代码

//  TimeViewController.m

/*

 *  App的开发中,会遇到一些要计算时间差的问题,比如:在友盟统计的列子里面,要统计一个页面从创建到销毁的时间或者统计用户在一个页面停留的时间。例如:我们数据缓存的情况下,要求在一个小时后从新刷新等情况。我们都要计算时间的间隔。方法有:时间戳 CFAbsoluteTimeGetCurrent。我们两个都实践。

 */

/*

 区分:

      NSDate CFAbsoluteTimeGetCurrentCACurrentMediaTime 的区别

 框架层:

 NSDate 属于Foundation

 CFAbsoluteTimeGetCurrent() 属于 CoreFoundatio

 CACurrentMediaTime() 属于 QuartzCore

 

 本质区别:

 NSDate CFAbsoluteTimeGetCurrent() 返回的时钟时间将会会网络时间同步,从时钟 偏移量的角度,mach_absolute_time() CACurrentMediaTime() 是基于内建时钟的,能够更精确更原子化地测量,并且不会因为外部时间变化而变化(例如时区变化、夏时制、秒突变等),但它和系统的uptime有关,系统重启后CACurrentMediaTime()会被重置。

 常见用法:

 NSDateCFAbsoluteTimeGetCurrent()常用于日常时间、时间戳的表示,与服务器之间的数据交互

 其中 CFAbsoluteTimeGetCurrent() 相当于[[NSDate data] timeIntervalSinceReferenceDate];

 CACurrentMediaTime() 常用于测试代码的效率

 */

//  Created by MAC on 16/9/29.

//  Copyright © 2016 NetworkCode小贱. All rights reserved.

//


#import "TimeViewController.h"


@interface TimeViewController ()

{

    /* 创建两个时间变量 开始 结束 */

    CFAbsoluteTime  StartTime;

    CFAbsoluteTime  EndTime;

    

    NSTimeInterval  ChuoStartTime ;

    NSTimeInterval  ChuoEndTime ;

}

/* 时间戳开始计时的时间*/

@property (weak, nonatomic) IBOutlet UILabel *ChuoStartLabel;

/* 时间戳结束的时间*/

@property (weak, nonatomic) IBOutlet UILabel *ChuoEndLabel;

/* 时间戳计算结果*/

@property (weak, nonatomic) IBOutlet UILabel *ChuoResultLabel;


/* CFAbsoluteTimeGetCurrent 开始时间*/

@property (weak, nonatomic) IBOutlet UILabel *CFStartLabel;

/* CFAbsoluteTimeGetCurrent 结束时间*/

@property (weak, nonatomic) IBOutlet UILabel *CFEndLabel;

/* CFAbsoluteTimeGetCurrent 计算时间差*/

@property (weak, nonatomic) IBOutlet UILabel *CFResultLbael;


/* 开始计时*/

- (IBAction)StartBtn:(id)sender;

/* 结束计时,并计算时间差*/

- (IBAction)EndBtn:(id)sender;


@end


@implementation TimeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

}

#pragma mark 获取开始时间  

-(void)getStartTime{

    /*  CFAbsoluteTimeGetCurrent 的方式  */

    StartTime = CFAbsoluteTimeGetCurrent();

    self.CFStartLabel.text = [NSString stringWithFormat:@"%0.3f",StartTime];

    self.CFStartLabel.adjustsFontSizeToFitWidth = YES;

    

    /* 时间戳的方法获取时间*/

    ChuoStartTime = [[NSDate date] timeIntervalSince1970];

    self.ChuoStartLabel.text = [NSString stringWithFormat:@"%f",StartTime];

    self.ChuoStartLabel.adjustsFontSizeToFitWidth = YES;

    /* 注意

       timeIntervalSince1970  来获取当前时间戳是对的,而 timeIntervalSinceNow 获取的时间戳是错误的

     */

}

#pragma mark 获取结束时间

-(void)getEndTime{

    /*  CFAbsoluteTimeGetCurrent 的方式  */

    EndTime = CFAbsoluteTimeGetCurrent();

    self.CFEndLabel.text = [NSString stringWithFormat:@"%0.3f",EndTime];

    self.CFEndLabel.adjustsFontSizeToFitWidth = YES;

    

    /* 时间戳的方法获取时间*/

    ChuoEndTime = [[NSDate date] timeIntervalSince1970];

    self.ChuoEndLabel.text = [NSString stringWithFormat:@"%f",StartTime];

    self.ChuoEndLabel.adjustsFontSizeToFitWidth = YES;

    [self computingTime];

    

    

}

#pragma mark 计算时间差

-(void)computingTime{

    CFAbsoluteTime ResultTime = EndTime - StartTime;

    self.CFResultLbael.text = [NSString stringWithFormat:@"%0.3f",ResultTime];

    self.CFResultLbael.adjustsFontSizeToFitWidth = YES;


    /* 时间戳的方法获取时间*/

    NSTimeInterval ChuoResultTime = ChuoEndTime - ChuoStartTime;

    self.ChuoResultLabel.text = [NSString stringWithFormat:@"%f",ChuoResultTime];

    self.ChuoResultLabel.adjustsFontSizeToFitWidth = YES;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


- (IBAction)StartBtn:(id)sender {

    /*  CFAbsoluteTimeGetCurrent 的方式  */

    [self getStartTime];

}


- (IBAction)EndBtn:(id)sender {

    /*  CFAbsoluteTimeGetCurrent 的方式  */

    [self getEndTime];

}

@end







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值