测试 代码运行时间

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 统计 程序 运行时间的更多相关文章

  1. 在 Linux 如何优雅的统计程序运行时间?恕我直言,你运行的可能是假 time

    最近在使用 time 命令时,无意间发现了一些隐藏的小秘密和强大功能,今天分享给大家. time 在 Linux 下是比较常用的命令,可以帮助我们方便的计算程序的运行时间,对比采用不同方案时程序的运行 ...

  2. Java统计程序运行时间

    代码如下: 第一种是以毫秒为单位计算的. long startTime = System.currentTimeMillis();    //获取开始时间 doSomething();    //测试 ...

  3. C++统计程序运行时间代码片段

    因为经常需要统计代码的运行时间,所以计时功能就显得很重要, 记录一下现在喜欢用的计时方式,供日后查阅. 1.下面是计时主函数, bool TimeStaticMine(int id,const cha ...

  4. Spark中统计程序运行时间

    import java.text.SimpleDateFormat import java.util.Date val s=NowDate() //显示当前的具体时间 val now=new Date ...

  5. ARTS-S c语言统计程序运行时间

    #include <stdio.h> #include <sys/time.h> #include <unistd.h> int main() { struct t ...

  6. VC中监测程序运行时间(二)-毫秒级

    /* * 微秒级计时器,用来统计程序运行时间 * http://blog.csdn.net/hoya5121/article/details/3778487#comments * //整理 [10/1 ...

  7. linux下统计程序/函数运行时间(转)

    一. 使用time 命令 例如编译一个hello.c文件 #gcc hello.c -o hello 生成了hello可执行文件,此时统计该程序的运行时间便可以使用如下命令 #time ./hello ...

  8. 第六章第一个linux个程序:统计单词个数

    第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数.  第 1 步:建立 Linu x 驱 ...

  9. 嵌入式linux应用程序调试方法

    嵌入式linux应用程序调试方法 四 内存工具 五 C/C++代码覆盖.性能profiling工具 四 内存工具 您肯定不想陷入类似在几千次调用之后发生分配溢出这样的情形. 许多小组花了许许多多时间来 ...

随机推荐

  1. Java多线程系列 JUC锁07 ConditionObject分析

    ConditionObject ConditionObject是AQS中的内部类,提供了条件锁的同步实现,实现了Condition接口,并且实现了其中的await(),signal(),signalA ...

  2. Spark Structured Streaming框架(5)之进程管理

    Structured Streaming提供一些API来管理Streaming对象.用户可以通过这些API来手动管理已经启动的Streaming,保证在系统中的Streaming有序执行. 1. St ...

  3. HTTPSQS(HTTP Simple Queue Service)消息队列

    HTTPSQS(HTTP Simple Queue Service)是一款基于 HTTP GET/POST 协议的轻量级开源简单消息队列服务,使用 Tokyo Cabinet 的 B+Tree Key ...

  4. Spring Cloud之Ribbon与Nginx区别

    客户端负载均衡器 在SpringCloud中Ribbon负载均衡客户端,会从eureka注册中心服务器端上获取服务注册信息列表,缓存到本地. 让后在本地实现轮训负载均衡策略. Ribbon与Nginx ...

  5. 源码分享-纯CSS3实现齿轮加载动画

    纯CSS3实现齿轮加载动画是一款可以用来做Loading动画的CSS3特效代码. 有兴趣的朋友可以下载下来试试:http://www.huiyi8.com/sc/8398.html

  6. PL/SQL Developer 的 SQL 编辑窗口显示行号

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一直奇怪为什么 PL/SQL 6 系列的版本可以显示行号,为什么到了 7 .8 版本之后反而还不行了?而且我都已经设置了“显示行号”的呀. 如图: ...

  7. DIV CSS 笔记

    /*针对谷歌浏览器内核支持的CSS样式*/ <style type="text/css"> @media screen and (-webkit-min-device- ...

  8. GridView设置多个DatakeyNames

    1.aspx页面GridView直接绑定DataKeyNames aspx设置: <asp:GridView ID="grvGrid" runat="server& ...

  9. QWidget上下文菜单处理函数

    QWidget类是Qt中所有可视化部件的基类,其很多函数都是虚函数,能被子类重写以表现不同形式和功能,今天来学习一下QWdiget的事件处理函数. 事件是鼠标.键盘或系统因其自身某些状态发生改变而引起 ...

  10. PS 滤镜— —Twirl Filter

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...