linux 统计 程序 运行时间
测试 代码运行时间
linux 中的 <sys/time.h> 中 有个函数可以获取当前时间,精确到 微秒 ----> gettimeofday()

#include <sys/time.h>
// int gettimeofday(struct timeval *tv, struct timezone *tz);
/*********************************************
* struct timeval
* {
* time_t tv_sec; // seconds
* suseconds_t tv_usec; // microseconds:微秒 10^(-6)s, 这里的 tv_sec 用的是 微秒
* // millisecond :毫秒 10^(-3)s
* }
**********************************************
* struct timezone
* {
* int tz_minuteswest; // minutes west of Greenwich
* int tz_dsttime; // type of DST correction
* }
**********************************************/

使用时,定义两个 struct timeval 变量(通常 gettimeofday() 的第二个参数 设为 NULL),分别保存 代码测试 前后的时刻,最后相减,即可获取 代码运行时间 (可转换为自己需要的时间)。

#include <stdio.h>
#include <sys/time.h> // for gettimeofday()
#include <string.h> // for memset() int main()
{
int i = ;
struct timeval start, end; // define 2 struct timeval variables //-------------------------
gettimeofday(&start, NULL); // get the beginning time
//------------------------- // test code
while(i)
{
i--;
} //-------------------------
gettimeofday(&end, NULL); // get the end time
//------------------------- long long total_time = (end.tv_sec - start.tv_sec) * + (end.tv_usec - start.tv_usec); // get the run time by microsecond
printf("total time is %lld us\n", total_time);
total_time /= ; // get the run time by millisecond
printf("total time is %lld ms\n", total_time);
} 测试结果:(CentOS 6.5, gcc 4.4.7)
total time is 49658 us
total time is 49 ms
linux 统计 程序 运行时间的更多相关文章
- 在 Linux 如何优雅的统计程序运行时间?恕我直言,你运行的可能是假 time
最近在使用 time 命令时,无意间发现了一些隐藏的小秘密和强大功能,今天分享给大家. time 在 Linux 下是比较常用的命令,可以帮助我们方便的计算程序的运行时间,对比采用不同方案时程序的运行 ...
- Java统计程序运行时间
代码如下: 第一种是以毫秒为单位计算的. long startTime = System.currentTimeMillis(); //获取开始时间 doSomething(); //测试 ...
- C++统计程序运行时间代码片段
因为经常需要统计代码的运行时间,所以计时功能就显得很重要, 记录一下现在喜欢用的计时方式,供日后查阅. 1.下面是计时主函数, bool TimeStaticMine(int id,const cha ...
- Spark中统计程序运行时间
import java.text.SimpleDateFormat import java.util.Date val s=NowDate() //显示当前的具体时间 val now=new Date ...
- ARTS-S c语言统计程序运行时间
#include <stdio.h> #include <sys/time.h> #include <unistd.h> int main() { struct t ...
- VC中监测程序运行时间(二)-毫秒级
/* * 微秒级计时器,用来统计程序运行时间 * http://blog.csdn.net/hoya5121/article/details/3778487#comments * //整理 [10/1 ...
- linux下统计程序/函数运行时间(转)
一. 使用time 命令 例如编译一个hello.c文件 #gcc hello.c -o hello 生成了hello可执行文件,此时统计该程序的运行时间便可以使用如下命令 #time ./hello ...
- 第六章第一个linux个程序:统计单词个数
第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数. 第 1 步:建立 Linu x 驱 ...
- 嵌入式linux应用程序调试方法
嵌入式linux应用程序调试方法 四 内存工具 五 C/C++代码覆盖.性能profiling工具 四 内存工具 您肯定不想陷入类似在几千次调用之后发生分配溢出这样的情形. 许多小组花了许许多多时间来 ...
随机推荐
- [原创]java WEB学习笔记03:使用eclipes开发javaWEB项目
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 暑假集训第一周比赛C题
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83146#problem/C C - 学 Crawling in process... C ...
- 亚马逊EC2服务器登录方法
1.根据官网提供的方法登录连接到EC2服务器(官网推荐windows用户使用PUTTY连接) 2. 创建root的密码,输入如下命令: sudo passwd root 3.然后会提示你输入new p ...
- Mysql Java 驱动安装
怎么安装MYSQL的JDBC驱动 1.下载mysql for jdbc driver. http://dev.mysql.com/downloads/connector/j/5.0.html 2.解压 ...
- 大话设计模式--代理模式 proxy
1. 代理模式: 为其他对象提供一种代理以控制这个对象的访问. 代理模式使用场合: a. 远程代理, 为一个对象在不同的地址空间提供局部代理,隐藏一个对象存在于不同地址空间的事实.如.net中WebS ...
- bzoj 2005: [Noi2010]能量采集 筛法||欧拉||莫比乌斯
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MB[Submit][Status][Discuss] Description 栋栋 ...
- 修改myEclipse2014web项目名称
重命名项目名称后 右键点击你的项目,然后选择属性---->然后点击myeclipse—>Project Facets—> web 选项,修改web context-root名称为你要 ...
- spring boot: 一般注入说明(四) Profile配置,Environment环境配置 @Profile注解
1.通过设定Environment的ActiveProfile来设置当前context所需要的环境配置,在开发中使用@Profile注解类或方法,达到不同情况下选择实例化不同的Bean. 2.使用jv ...
- Twitter的流处理器系统Heron——升级的storm,可以利用mesos来进行资源调度
2011年,Twitter发布了开源的分布式流计算系统Storm.四年后,随着用户数量的急剧增加,Twitter每天要处理的事件已经增加到十亿以上.Storm系统应对如此庞大而复杂多样的流数据变得十分 ...
- Selenium-使用firepath获取元素的xpath