精确获取函数运行时间,精确到微秒

Linux或者VxWorks下面精确获取函数运行的时间:

uint64 start_time;    /* 起始时间 */

uint64 end_time;    /* 结束时间 */

struct timespec ltv;
(void)clock_gettime(CLOCK_REALTIME, &ltv);
start_time = (uint64)ltv.tv_sec * 1000 * 1000 + (uint64)ltv.tv_nsec / 1000; /* (us) */

/* 待测函数 */

(void)clock_gettime(CLOCK_REALTIME, &ltv);
end_time = (uint64)ltv.tv_sec * 1000 * 1000 + (uint64)ltv.tv_nsec / 1000; /* (us) */

总结:

Linux系统中获取时间,精确到秒级以下的有两个,一个是timespec,一个是timeval。两者的具体定义和差别可以参考如下。

一,struct timespec

系统中该结构体定义如下,

typedef long time_t;

#ifndef _TIMESPEC

#define _TIMESPEC

struct timespec{

time_t tv_sec;    /* seconds 秒 */

long tv_nsec;    /* nanoseconds 纳秒 */

};

#endif

struct timespec有两个成员,一个是秒,一个是纳秒,所以最高精确度是纳秒。

一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:

CLOCK_REALTIME             系统当前时间,从1970年1.1日算起

CLOCK_MONOTONIC            系统的启动时间,不能被设置

CLOCK_PROCESS_CPUTIME_ID   本进程运行时间

CLOCK_THREAD_CPUTIME_ID    本线程运行时间

二,struct timeval

struct timeval{

time_t tv_sec;    /* seconds 秒 */

long tv_usec;    /* microseconds 微秒 */

};

struct timezone{

int tz_minuteswest;   /* minutes west of Greenwich */

int tz_dsttime;       /* type of DST correction */

};

struct timeval 有两个成员,一个是秒,一个是微秒,所以最高精确度是微秒。

一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统时间。

时间: 2024-08-29 06:58:30

精确获取函数运行时间,精确到微秒的相关文章

VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm.Format("现在时间是%Y年%m月%d日 %X"); MessageBox(str,NULL,MB_OK); a,从CTimet中提取年月日时分秒 CTime t = CTime::GetCurrentTime(); int d=t.GetDay(); //获得几号 int y=t.

精确获取时间(QueryPerformanceCounter)

LARGE_INTEGER tima,timb; QueryPerformanceCounter(&tima); 在 Windows Server 2003 和 WindowsXP 中使用 QueryPerformanceCounter 函数的程序可能执行不当 QueryPerformanceCounter 來精確計算執行時間QueryPerformanceCounter 來精確計算執行時間// 這個程式展示了如何使用QueryPerformanceCounter 來精確計算執行時間//代码 1

【转】c++ 获取程序运行时间

转自:http://blog.csdn.net/ghevinn/article/details/22800059 DWORD start_time=GetTickCount(); {...} DWORD end_time=GetTickCount(); DWORD Subtime = (end_time-start_time); int k = 0; 如何获取代码运行时间 在调试中,经常需要计算某一段代码的执行时间,下面给出两种常用的方式: 第一种:使用GetTickCount函数 #inclu

时间获取函数封装--linux/unix

项目开发中在提高程序效率时,首先要定位到代码那个部分耗费时间较长,进而改进代码--缩短执行时间.这就要一个时间获取函数来准确获取时间,在linux/unix系统都有对应的时间获取函数,不过要对其转换封装才可以得到我们想要且看得懂的数值,下例是以封装gettimeofday()函数和struct timeval结构体获取的时间获取函数. #include <sys/time.h>    #include <unistd.h> 定义函数:int gettimeofday (struct

python3打印当前时间和获取程序运行时间

学习使用time模块和datetime模块. 通常我们想让程序等待几秒钟,再继续向下运行,time模块的sleep()方法是一个很好的选择.但是想通过time模块打印系统的当前时间,则比较麻烦.如下: from time import strftime, localtime # 打印当前时间 def printTime(): print(strftime("%Y-%m-%d %H:%M:%S", localtime())) return 需要调用两个函数. 通过time模块获取程序运行

Webkit的自定义属性获取函数以及属性删除函数实现

概述: [CustomEnumerateProperty] 当给定的接口被枚举时,允许你为指定接口的属性获取函数编写自己的实现. 同样,当接口的属性被删除时,[CustomDeleteProperty]允许你编写自己的实现. customEnumerateProperty](i), [CustomDeleteProperty](i) 用法: 这两个修饰可作用在interface,用法如下: [ CustomEnumerateProperty, CustomDeleteProperty ] int

[C.Sharp] TimeSpan的用法,获取测试程序运行时间

TimeSpan的用法 TimeSpan是用来表示一个时间段的实例,两个时间的差可以构成一个TimeSpan实例,现在就来简单介绍一下几点重要的用法: a 先来介绍几个方法 TimeSpan.Minutes(其他时间比如天数,小时数,秒数都一样的情况下得到的分钟数的差),其他的Hours,Second一样 DateTime.Tick :是一个计时周期,表示一百纳秒,即一千万分之一秒,那么 Ticks 在这里表示总共相差多少个时间周期,即:9 * 24 *                    3

javascript 获取函数形参个数

分享下javascript获取函数形参个数的方法. /** * 获取函数的形参个数 * @param {Function} func [要获取的函数] * @return {*} [形参的数组或undefind]* @site www.jbxue.com */function getFuncParameters(func) { if (typeof func == 'function') { var mathes = /[^(]+\(([^)]*)?\)/gm.exec(Function.pro

php -- 获取函数参数

----- 015-parameter.php ----- 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 5 <title>获取函数参数</title> 6 </head> 7 <body> 8 <h3