linux计算程序运行时间

转自:

http://www.cnblogs.com/NeilHappy/archive/2012/12/08/2808417.html

#include <sys/time.h> int gettimeofday(struct timeval *tv,struct timezone *tz); strut timeval { long tv_sec; /* 秒数 */ long tv_usec; /* 微秒数 */ }; gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替。

以下是程序:#i nclude <sys/time.h> #i nclude <stdio.h> #i nclude <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); }

//这个程序在我的电脑运行的结果大概为0.03 - 0.04s
时间: 12-21

linux计算程序运行时间的相关文章

c++ 计算程序运行时间

转载 http://blog.csdn.net/trustbo/article/details/10582287 以前经常听人提起如何计算程序运行时间,给出一系列函数,当时没有注意,随便选了clock()最简单的方式进行计算.等到真正需要检测程序性能提升了多少,才发现这里面有很多要注意的地方. 最常用的的方式: #include time_t start = clock(); time_t end = clock(); printf("the running time is : %f\n&quo

linux计算程序所用时间

######################################################################### # File Name: testTime.sh # Author: fangtest # mail: [email protected]126.com # Created Time: Sun 29 Jun 2014 06:42:36 PM EDT ###################################################

C/C++可以用的计算程序运行时间的方法

#include"head_file.h" #include<sys/timeb.h> #include<stdio.h> int main() { timeb t1, t2;//定义两个变量 long t; double x, sum = 0, sum1=1; int i, j, n; printf("请输入x n:"); scanf("%lf %d",&x,&n); ftime(&t1);//记

Python 计算程序运行时间

import time def start_sleep():    time.sleep(3) if __name__ == '__main__':    #The start time     start = time.clock() #A program which will run for 3 seconds    start_sleep() #The End time     end = time.clock() print("The function run time is : %.0

C++获取当前时间和计算程序运行时间的方法

C++获取当前时间和计算程序运行时间的方法 获取当前时间: #include <iostream> #include <Windows.h> using namespace std; int main() { SYSTEMTIME sys; GetLocalTime(&sys); cout<<sys.wYear<<"年"; cout<<sys.wMonth<<"月"; cout<&

Delphi计算程序运行时间

1 var 2 StartTime, EndTime: cardinal; 3 begin 4 StartTime := GetTickCount; 5 sleep(1000); 6 EndTime := GetTickCount; 7 caption := (IntToStr(EndTime - StartTime) + ' ms'); 8 end; 转:Delphi 计算程序运行时间 http://www.delphitop.com/html/chengxu/2882.html 原文地址:h

Windows 计算程序运行时间(高精度计时)

首先,认识一下clock()和GetTickCount(): 一.clock()clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:clock_t clock(void) ;简单而言,就是该程序从启动到函数调用占用CPU的时间.这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock):若挂钟时间不可取,则返回-1.

Python计算程序运行时间

方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datetime.datetime.now() print (endtime - starttime).seconds 方法 2 start = time.time() run_fun() end = time.time() print end-start 方法3 start = time.clock() run_fun() end =

java如何计算程序运行时间

long startTime = System.currentTimeMillis();    //获取开始时间 doSomething();    //测试的代码段 long endTime = System.currentTimeMillis();    //获取结束时间 System.out.println("程序运行时间:" + (endTime - startTime) + "ms");    //输出程序运行时间 第二种是以纳秒为单位计算的. long