C语言–计算程序执行时间
1. gettimeofday
精度1us

#include<stdio.h>
#include<sys/time.h>

int main()
{
/* 定义两个结构体 */
struct timeval start;
struct timeval end;
unsigned long timer;
/* 程序开始之前计时start */
gettimeofday(&start, NULL);
printf("hello world!\n");
/* 程序块结束后计时end */
gettimeofday(&end, NULL);
/* 统计程序段运行时间(unit is usec)*/
timer = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
printf("timer = %ld us\n", timer);
return 0;
}

输出结果:

$ ./time
hello world!
timer = 47 us

2. clock
精度能达到1us

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
clock_t begin, end;
double cost;
//开始记录
begin = clock();
/*待测试程序段*/
printf("hello world!\n");
//结束记录
end = clock();
cost = (double)(end - begin)/CLOCKS_PER_SEC;
printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

输出结果www.cdxsxbx.com

$ ./time
hello world!
constant CLOCKS_PER_SEC is: 1000000, time cost is: 0.000042 secs

3. time命令
$ time ls
0.00s user 0.00s system 0% cpu 0.001 total

user时间是指进程花费在用户模式中的CPU时间,这是唯一真正用于执行进程所花费的时间,其他进程和花费阻塞状态中的时间没有计算在内。
sys时间是指花费在内核模式中的CPU时间,代表在内核中执系统调用所花费的时间,这也是真正由进程使用的CPU时间。
total指进程时间之和。精度1ms

C语言--计算程序执行时间的更多相关文章

  1. Java 获取并计算程序执行时间

    一般输出日期时间经常会用到Date这个类: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ...

  2. C# stopwatch的简单使用(计算程序执行时间)

    首先添加引用 using System.Diagnostics;//stopwatch的引用 //声明变量 Stopwatch a=new Stopwatch();//PS:这里一定要new(实例化) ...

  3. C语言计算程序运行时间

    #include<stdio.h>#include<stdlib.h> #include "time.h" int main( void )  {     ...

  4. R语言-程序执行时间

    我们往往对自己编写程序的运行效率十分关心,需要查看程序的执行时间. 在R中,获得时间的函数有不少,比如system.time().proc.time()等. 个人使用较多的是proc.time() & ...

  5. c++中计算程序执行时间

    #include<iostream> #include<time.h> using namespace std; int main() { clock_t t1 = clock ...

  6. linux c 编程 ------ 获取时间,计算程序执行时间

    #include <time.h> #include <stdio.h> #include <unistd.h> int main(int argc, char a ...

  7. c语言统计程序执行时间

    c语言程序执行时间 #include <iostream> #include <cstdio> #include <ctime> int main() { std: ...

  8. Python时钟,计算程序运行时间

    关于计算程序执行时间 import time def sleep(): time.sleep(2.5) def forloop(count): for i in range(count): print ...

  9. Python小白学习之路(二十四)—【装饰器】

    装饰器 一.装饰器的本质 装饰器的本质就是函数,功能就是为其他函数添加附加功能. 利用装饰器给其他函数添加附加功能时的原则: 1.不能修改被修饰函数的源代码        2.不能修改被修饰函数的调用 ...

随机推荐

  1. day97_11_29

    一.数据存储到mongodb 爬取的数据如果需要存储到mongodb中,要通过item,定义一个存储类.再yield一个类. 数据存储的时候需要进过pipelines,再到setting中配置. fr ...

  2. 20191214 Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    概述 切了 ABCE,Room83 第一 还行吧 A - Happy Birthday, Polycarp! 题解 显然这样的数不会很多. 于是可以通过构造法,直接求出 \([1,10^9]\) 内所 ...

  3. mac怎么连接windows远程桌面

    首先需要下载一个软件,因为苹果电脑并没有提供免费的软件给我们,所以不能像windows一样, 直接在任务管理中搜素远程桌面然后输入ip地址,用户名,密码就可以远程连接, 而苹果也有提供一个软件,但要付 ...

  4. SpringCloud微服务(01):Eureka组件,管理服务注册与发现

    本文源码:GitHub·点这里 || GitEE·点这里 一.Eureka基本架构 1.Eureka简介 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,SpringCl ...

  5. 拥抱webpack4,有效缩减构建时间57%+

    背景 最近有感觉到,随着系统模块数量的增加,wepack编译打包的速度越来越慢,于是我想给项目做一下优化升级,也借此机会系统地学习一下webpack4. 升级过程 当前版本 "depende ...

  6. [算法]PriorityQueue的应用

    1. 数据流中的第K大元素 题目 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组 ...

  7. 数据显示按规格向datatable中增加空白记录

    /// <summary> /// 按前台分页样式为datatable增加空行 /// </summary> /// <param name="gridPage ...

  8. python-Redis模块常用的方法汇总

    Redes模块常用的方法汇总 一.创建建Redis对象 1.直接使用 import redis r = redis.Redis(host='127.0.0.1', port=6379) 2.连接池使用 ...

  9. 【原创】REPORT自动生成工具

    ---------------------------------------------- 本博客所有原创文章,未经博主允许,请勿转载. ------------------------------ ...

  10. python中字典

    字典中key:不可改变的数据类型 #fromkeys 快速定义一个空字典 res = {}.fromkeys([']) print(res) 定义字典: dict1 = { 'name1':'天明', ...