clock_gettime测代码运行时间
//函数原型:
// long clock_gettime (clockid_t which_clock, struct timespec *tp);
//参数列表:
// CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变。
// CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
// CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
// CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
//返回值:
// 0:成功,-1:错误,在errno中保存错误代码 //目的:测代码运行时间 #include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h> void diff(struct timespec *start, struct timespec *end, struct timespec *interv)
{
if((end->tv_nsec - start->tv_nsec) < 0)
{
interv->tv_sec = end->tv_sec - start->tv_sec-1;
interv->tv_nsec = 1000000000 + end->tv_nsec - start->tv_nsec;
}else
{
interv->tv_sec = end->tv_sec - start->tv_sec;
interv->tv_nsec = end->tv_nsec - start->tv_nsec;
}
return;
} void curr_time(struct timespec *time)
{
clock_gettime(CLOCK_REALTIME, time);
} int main()
{
struct timespec start, end, interv; curr_time(&start);
//do something here
curr_time(&end);
diff(&start, &end, &interv);
printf("cost time nsec %ld.\n",interv.tv_sec * 1000000000 + interv.tv_nsec);
}
clock_gettime测代码运行时间的更多相关文章
- C#如何测试代码运行时间
1.System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 // 需要测试 ...
- 使用console进行 性能测试 和 计算代码运行时间(转载)
本文转载自: 使用console进行 性能测试 和 计算代码运行时间
- C# 测试代码运行时间
一.新建一个控制台程序项目Test.exe using System; using System.Collections.Generic; using System.Linq; using Syste ...
- C# 精准获取代码运行时间
纯粹转载,转载请注明参考链接及作者! 参考链接:http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html,作者:博客园 大佬辉 ...
- Objective-C 计算代码运行时间
今天看到一篇关于iOS应用性能优化的文章,其中提到计算代码的运行时间,觉得非常有用,值得收藏.不过在模拟器和真机上是有差异的,以此方法观察程序运行状态,提高效率. 第一种:(最简单的NSDate) N ...
- Java精确测量代码运行时间
Java精确测量代码运行时间: --------------- long startTime = System.nanoTime(); //開始時間 for(int i = 0;i<10000; ...
- Python之自测代码标识__name__=='__main__'
__name__是python的默认的自测代码标识,其他文件导入该python文件时,不会执行这行代码以下部分. def yangfan(a): print('yangfan %s' %a) prin ...
- 用SWD调试接口测量代码运行时间 ( SWO )
用SWD调试接口测量代码运行时间 关于时间测量的种种问题 在嵌入式中,我们经常需要测量某段代码的执行时间或测量事件触发的时间,常规的思路是: 1:在测量起始点,反转电平2:在测量结束点,再次反转电平 ...
- C# JAVA 记录代码运行时间
C# System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 // cod ...
随机推荐
- PHP表单常用正则表达式(URL、HTTP、手机、邮箱等)
<?php /** * @description: 正则表达式匹配 */ class Regex { /** * @手机号 */ public static function Phone($su ...
- jquery技巧(持续更新。。)
(1)集合处理功能 //为索引为0,1,2的元素分别设置不同的字体颜色 $('p').each(function(i){ this.styl ...
- php函数声明的简单实例
<?phpecho table(10,5,500);echo table(2,2,400); //函数调用 function table($row,$col,$width){ //通过函数标记t ...
- 【python之旅】python的模块
一.定义模块: 模块:用来从逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能),本质就是以.py结尾的python文件(文件名:test.py ,对应的模块名就是test) 包:用来从逻 ...
- JDK源码阅读(一) ArrayList
基于JDK7.0 ArrayList<E>类继承了抽象类AbstractList<E> 实现了List<E> 接口,RandomAccess接口,Cloneable ...
- 应用git(SSH设置)
git配置完成email和user后,才可以生成SSH Key. $ git config --global user.name "自定义名称" $ git config --gl ...
- CMD 模块定义规范
在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...
- eclipse常见错误
1.The superclass “javax.servlet.http.httpservlet” is not found in the build path 原因:未添加server librar ...
- Hibernate(六)一对一双向关联映射
在上次的博文Hibernate从入门到精通(五)一对一单向关联映射中我们讲解了一下一对一单向关联映射, 这次我们继续讲解一下与之对应的一对一双向关联映射. 一对一双向关联 与一对一单向关联映 射所不同 ...
- Linux下用户及用户组的管理
一.用户账号管理 1. 添加新用户账号 命令格式: useradd 选项 用户名 选项含义如下: -c comment 指定一段注释性描述. -d 目录 指定用户主目录,如果此目录不存在,则同时使用- ...