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. java中使用lambda表达式

    使用lambda表达式能够使复杂的编写方式变的简单 lambda表达式的语法 (parameters) -> expression 或 (parameters) ->{ statement ...

  2. 【bzoj5339】[TJOI2018]教科书般的亵渎(拉格朗日插值/第二类斯特林数)

    传送门 题意: 一开始有很多怪兽,每个怪兽的血量在\(1\)到\(n\)之间且各不相同,\(n\leq 10^{13}\). 然后有\(m\)种没有出现的血量,\(m\leq 50\). 现在有个人可 ...

  3. 八、VTK安装并运行一个例子

    一.版本 win10 VS2019 VTK8.2.0 其实vtk的安装过程和itk的安装过程很是类似,如果你对itk的安装很是熟悉(也就是我的博客一里面的内容,那么自己就可以安装.) 如果不放心,可以 ...

  4. C++ explicit关键字,修饰构造函数,ctor

    #include <iostream> // operator Type() 类型操作符重载 // operator int() // operator double() // ... / ...

  5. angularjs中directive指令与component组件有什么区别?

     壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...

  6. Java基础(七)

    左连接,右连接,内连接,全连接的区别 左连接:返回左表所有行,右表没有匹配行则返回null 右连接:返回右表所有行,左表没有匹配行则返回null 内连接:返回左右表共有行 全连接:返回左右表所有行,无 ...

  7. 一起学Android之音频视频

    概述 Android多媒体框架支持各种常见的媒体类型,可以很容易地将音频.视频和图像集成到App中.通过MediaPlayer Api,可以从应用程序资源(RAW).文件系统或网络上数据流资源来播放音 ...

  8. CAD转PDF的软件哪个比较好用?用这两个很方便

    大家都知道编辑CAD图纸是需要借助CAD制图软件来进行绘制的,而且CAD制图软件很多的设计师们都在使用.但是CAD中的图纸格式为dwg格式的,不想要使用CAD软件来查看图纸的话,就需要将CAD转换成P ...

  9. jquery实现get的异步请求

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><html& ...

  10. 利用socket传递图片

    package com.company.s3; import java.io.File; import java.io.FileOutputStream; import java.io.InputSt ...