测试 代码运行时间

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. CKeditor插件开发流程(二)SyntaxHighlighter

    CKEditor整合SyntaxHighlighter实现代码高亮显示 1,版本说明 CKEditor:ckeditor_4.0.1_standard.zipSyntaxHighlighter:syn ...

  2. python 3 协程函数

    python 3 协程函数 1:把函数的执行结果封装好__iter__和__next__,即得到一个迭代器 2:与return功能类似,都可以返回值,但不同的是,return只能返回一次值,而yiel ...

  3. Entity Framework在Asp.net MVC中的实现One Context Per Request(转)

    上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...

  4. Netty入门例子

    新建maven项目,添加依赖 <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency&g ...

  5. 十九 Django框架,发送邮件

    全局配置settings.py EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' #发送邮件引擎 EMAIL_USE_TLS ...

  6. 二 Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器

    Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器 这一节主讲url控制器 一.urls.py模块 这个模块是配置路由映射的模块,当用户访问一个 ...

  7. AngularJS学习笔记(三) 单页面webApp和路由(ng-route)

    就我现在的认识,路由($route)这个东西(也许可以加上$location)可以说是ng最重要的东西了.因为angular目前最重要的作用就是做单页面webApp,而路由这个东西是能做到页面跳转的关 ...

  8. vi 常用编辑命令

    什么是vi: vi是Linux/Unix底下最常用的文本编辑器,可以理解为和Windows下的txt一样,咱们一般操作linux服务器的时候都是没有图形化界面的, 怎么移动光标,到哪个位置,替换修改什 ...

  9. Java企业微信开发_01_接收消息服务器配置

    一.准备阶段 需要准备事项: 1.一个能在公网上访问的项目: 见:Java微信公众平台开发_01_本地服务器映射外网 2.一个企业微信账号: 去注册:(https://work.weixin.qq.c ...

  10. 3_observer

    #Observer 成就系统 achievements system 玩家完成某种成就后,通知监督者,监督者做出相应出来 ``` //简单来说就是事件触发的时候, 通知监督者 class Observ ...