• 使用Objective-C 计算代码运行时间


    第一种:(最简单的NSDate)

    NSDate* tmpStartData = [NSDate date];
    //You code here...
    double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData];
    NSLog(@"cost time = %f", deltaTime);
    

    第二种:(将运行代码放入下面的Block中,返回时间)

    #import <mach/mach_time.h>  // for mach_absolute_time() and friends  
      
    CGFloat BNRTimeBlock (void (^block)(void)) {  
        mach_timebase_info_data_t info;  
        if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;  
      
        uint64_t start = mach_absolute_time ();  
        block ();  
        uint64_t end = mach_absolute_time ();  
        uint64_t elapsed = end - start;  
      
        uint64_t nanos = elapsed * info.numer / info.denom;  
        return (CGFloat)nanos / NSEC_PER_SEC;  
    }
    

    第三种:

    /**
     * 计算脚本时间
     * @param $last 最后一次的运行clock
     * @param $key  标识
     * @return 当前clock
     */
    double t(double last, char* key){
        clock_t now = clock();
        printf("time:%fs 	 key:%s 
    ", (last != 0) ? (double)(now - last) / CLOCKS_PER_SEC : 0, key);
        return now;
    }
    
    double t1 = t(0, "");
    //do something
    t(t1, "end");



  • 相关阅读:
    vue 前端框架 (二) 表格增加搜索
    vue 前端框架
    数据结构-树的基本操作
    linux的串口驱动分析
    TTY驱动程序架构
    linux MTD系统解析(转)
    DM9000网卡的基本工作原理
    ok6410的LCD裸机范例
    ok6410的DMA裸机总结
    ok6410串口裸机总结
  • 原文地址:https://www.cnblogs.com/goodboy-heyang/p/5327641.html
Copyright © 2020-2023  润新知