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. C#中巧用妙法避免嵌套方式使用两个foreach循环

    问题:需要对DataGridViewRow的下拉框列Item2所选内容进行判断,看是否跟数据库里面某个配置表的数据列Item1匹配.如果用两个foreach循环进行匹配,会导致逻辑复杂而且容易只bre ...

  2. 什么是java变量,java变量是什么

    什么是变量,变量是什么 1.1. 什么是变量 在日常生活中,人们会用到大量数据,像去ATM机取款,首先,需要插入银行卡,这个过程其实就是ATM机的系统在获取银行卡号这个数据,而后,输入密码,这个过程也 ...

  3. 《工作笔记:移动web页面前端开发总结》

    工作笔记:移动web页面前端开发总结 移动web在当今的发展速度是一日千里,作为移动领域的门外汉,在这段时间的接触后,发现前端开发这一块做一个小小的总结. 1.四大浏览器内核 1.Trident (I ...

  4. office2019专业版激活秘钥 激活码

    office2019专业版激活码 激活秘钥 2019年5月更新 [Key]:F4QWT-NMMKH-XPTV9-W9HFB-B4JCQ [剩余次数:900000+] office2019激活 产品秘钥 ...

  5. Git 将已有项目推送到新建的远程仓库

    目录 一.需求: 二.较快捷的操作: 一.需求: 将一个本地的项目推送到一个新建的远程仓库中: 二.较快捷的操作: 1.创建一个远程仓库 以此为例:http://192.168.1.183/git/p ...

  6. 多次调用settimeout 如何使用单例模式

    <script> function aaa() { window.counter = window.counter||1; console.log(window.counter); win ...

  7. 关于大数据T+1执行流程

    关于大数据T+1执行流程 前提: 搭建好大数据环境(hadoop hive hbase sqoop zookeeper oozie hue) 1.将所有数据库的数据汇总到hive (这里有三种数据源 ...

  8. B站弹幕爬取 / jieba分词 - 全站第一的视频弹幕都在说什么?

    前言 本次爬取的视频av号为75993929(11月21的b站榜首),讲的是关于动漫革命机,这是一部超魔幻现实主义动漫(滑稽),有兴趣的可以亲身去感受一下这部魔幻大作. 准备工作 B站弹幕的爬取的接口 ...

  9. QAnet Encoder

    #!/usr/bin/python3# -*- coding: utf-8 -*-'''date: 2019/8/19mail: cally.maxiong@gmail.comblog: http:/ ...

  10. centos添加用户并赋予管理员权限

    用centos时,root用户一般都是超级管理员使用的,一般不轻易给别人,但是有时候同事安装软件时需要root账号,又不得不给,只能重新建一个用户,并赋予管理员权限.下面介绍创建用户并赋予管理员权限的 ...