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 hours, minutes, seconds, January , , Coordinated Univer-
sal Time, without including leap seconds. If an error occurs, time() returns the value (time_t)-. 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 = ;
while (*p != '\0')
{
sum = sum* + (*p-'');
p++;
}
return sum;
} int main(int argc, char* argv[]){
time_t start, stop; // time_t aka long
int def = ;
if (argc == )
{
def = str2int(argv[argc-]);
}
start = time(NULL);
for (int i = ; i < def ; i++)
{
float tmp = sqrt(i);
}
stop = time(NULL);
time_t total = stop - start; // 使用运行的时间 以秒为单位
printf("Start: %ld s\n", start);
printf("Stop: %ld s\n", stop);
printf("Time: %ld s\n", total); // 总共使用的时钟
return ;
}

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

最新文章

  1. ABP源码分析三十二:ABP.SignalR
  2. 解决升级Win 10 IP 10122后无法调试UAP应用的方法
  3. JNI环境搭建,CDT, cygwin,NDK
  4. STRUTS2 标签 循环次数
  5. Struts1 标签库 说明
  6. MARKDOWN--介绍http://www.jianshu.com/p/q81RER
  7. 剑指Offer06 旋转数组的最小值
  8. Java笔记(二十九)&hellip;&hellip;网络编程
  9. js中可转bool为false的值
  10. MapReduce 原理与 Python 实践
  11. Mysql简单笔记
  12. day09_request&amp;response学习笔记
  13. HDU 1495 非常可乐【BFS】
  14. MariaDB 连接查询与子查询(6)
  15. 新安装的ubuntu编辑器问题
  16. Java:多线程,CyclicBarrier同步器
  17. 2015.7.7js-07-2(基础)
  18. svn出错:directory &#39;xxxx&#39; is out of date
  19. Mysql建表语句
  20. fabric实现文本聚焦、可编辑

热门文章

  1. OD 快捷方式及窗口说明
  2. 【zabbix部署】基于linux安装zabbix监控服务和被监控服务
  3. vue3.0 本地调试时Invalid Host header
  4. Linux - CentOS 7 安装 .Net Core 运行环境
  5. C# 打开文件/跳转链接
  6. Class文件结构-练习题1
  7. 项目中遇到的问题:IDEA maven项目报错:程序包com.sun.image.codec.jpeg不存在
  8. 团队项目之测试与发布(Alpha版本)
  9. json属性里面出现特殊字符怎么获取属性
  10. MyBatis核心对象之StatementHandler