当前位置: 首页 > 图文教程 > 操作系统 > Unix/Linux > Linux下计算程序运行时间的两种方法

Unix/Linux
遭遇MYSON MTD803F
局域网虚拟服务器的工作模式
NFS Server+NFS Client配置
SmoothWall 防火墙(软路由)安装+配置详解
让Linux NAT firewall支持MSN Messenger
[精华] 使用 IGD(Upnp)解決 MSN 語音聊天問題
UPnP on your Linux 2.4 firewall how-to
Modules的概念及使用
Fedora 可以用了,下一步计划
Linux下的库(上)--如何使用非标准库
python遍历文件夹
PHP测试题
我的vimrc
自己写的备份服务器的脚本
破XP开机密码
调谐LINUX网络性能之调试工具篇
由vmstat看服务器
通过红旗4.1plus管窥红旗5.0(多图)
安装ff的误区+解决方法
网络安全:iptable

Unix/Linux 中的 Linux下计算程序运行时间的两种方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-01   浏览: 116 ::
收藏到网摘: n/a

1.以下是我在网上看到的:

有时候我们要计算程序执行的时间.比如我们要对算法进行时间分析

..这个时候可以使用下面这个函数.

#include <sys/time.h> int gettimeofday(struct timeval *tv,struct timezone *tz); strut timeval { long tv_sec; /* 秒数 */ long tv_usec; /* 微秒数 */ }; gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替. #include <sys/time.h< #include <stdio.h< #include <math.h< void function() { unsigned int i,j; double y; for(i=0;i<1000;i++) for(j=0;j<1000;j++) y=sin((double)i); } main() { struct timeval tpstart,tpend; float timeuse; gettimeofday(&tpstart,NULL); function(); gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec; timeuse/=1000000; printf("Used Time:%f\n",timeuse); exit(0); }

这个程序输出函数的执行时间,我们可以使用这个来进行系统性能的测试,或者是函数算

法的效率分析.在我机器上的一个输出结果是: Used Time:0.556070

2.第二种是我自己经常用的,就是:

在执行程序前,加time,如:输入time./abc

我也不知道为什么网上普遍介绍的是第一种方法……