• 程序运行时间测试


    c 标准库中,有time 函数,可以返回 1970年1月1日 开始到现在的秒数,我们可以调用两次的时间差来计算程序运行时间:

    https://github.com/yaowenxu/Workplace/blob/master/timer/timetimer.c

    NAME
         time -- get time of day
    
    LIBRARY
         Standard C Library (libc, -lc)
    
    SYNOPSIS
         #include <time.h>
    
         time_t
         time(time_t *tloc);
    
    DESCRIPTION
         The time() function returns the value of time in seconds since 0 hours, 0 minutes, 0 seconds, January 1, 1970, Coordinated Univer-
         sal Time, without including leap seconds.  If an error occurs, time() returns the value (time_t)-1.
    
         The return value is also stored in *tloc, provided that tloc is non-null.
    time函数说明
    /**
     * Author: Yaowen Xu
     * Github: https://github.com/yaowenxu
     * Organization: 北航系统结构研究所
     * Date: 2019-08-18 13:03:53
     * LastEditTime: 2019-08-18 13:14:33
     * Description: 使用 C 语言库 time 函数 对程序运行计时 以秒为单位
     */
    
    #include <time.h>
    #include <stdio.h>
    #include <math.h>
    
    int str2int(char* str){
        char *p = str;
        int sum = 0;
        while (*p != '')
        {
            sum = sum*10 + (*p-'0');
            p++;
        }
        return sum;
    }
    
    int main(int argc, char* argv[]){
        time_t start, stop; // time_t aka long 
        int def = 1000;
        if (argc == 2)
        {
            def = str2int(argv[argc-1]);
        }
        start = time(NULL);
        for (int i = 0; i < def ; i++)
        {
            float tmp = sqrt(i);
        }
        stop =  time(NULL);
        time_t total =  stop - start; // 使用运行的时间 以秒为单位
        printf("Start: %ld s
    ", start);
        printf("Stop: %ld s
    ", stop);
        printf("Time: %ld s
    ", total); // 总共使用的时钟
        return 0;
    }

    保持更新,如果对您有帮助,请点击推荐!更多关于C语言相关的知识,请关注 cnblogs.com/xuyaowen

  • 相关阅读:
    Java Image Processing
    贝塞尔曲线开发的艺术
    Ubuntu中Hadoop环境搭建
    FIRST集合、FOLLOW集合、SELECT集合以及预测分析表地构造
    Linux环境下使用VSCode编译makefile文件的注意事项
    神经记忆模型
    深度学习推荐阅读的论文
    博客园无法发布文章解决办法
    计算机各个方向名校公开课
    软件过程基础
  • 原文地址:https://www.cnblogs.com/xuyaowen/p/c-lib-time.html
Copyright © 2020-2023  润新知