//函数原型:
// 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测代码运行时间的更多相关文章

  1. C#如何测试代码运行时间

    1.System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 // 需要测试 ...

  2. 使用console进行 性能测试 和 计算代码运行时间(转载)

    本文转载自: 使用console进行 性能测试 和 计算代码运行时间

  3. C# 测试代码运行时间

    一.新建一个控制台程序项目Test.exe using System; using System.Collections.Generic; using System.Linq; using Syste ...

  4. C# 精准获取代码运行时间

    纯粹转载,转载请注明参考链接及作者! 参考链接:http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html,作者:博客园 大佬辉   ...

  5. Objective-C 计算代码运行时间

    今天看到一篇关于iOS应用性能优化的文章,其中提到计算代码的运行时间,觉得非常有用,值得收藏.不过在模拟器和真机上是有差异的,以此方法观察程序运行状态,提高效率. 第一种:(最简单的NSDate) N ...

  6. Java精确测量代码运行时间

    Java精确测量代码运行时间: --------------- long startTime = System.nanoTime(); //開始時間 for(int i = 0;i<10000; ...

  7. Python之自测代码标识__name__=='__main__'

    __name__是python的默认的自测代码标识,其他文件导入该python文件时,不会执行这行代码以下部分. def yangfan(a): print('yangfan %s' %a) prin ...

  8. 用SWD调试接口测量代码运行时间 ( SWO )

    用SWD调试接口测量代码运行时间 关于时间测量的种种问题 在嵌入式中,我们经常需要测量某段代码的执行时间或测量事件触发的时间,常规的思路是: 1:在测量起始点,反转电平2:在测量结束点,再次反转电平 ...

  9. C# JAVA 记录代码运行时间

    C# System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 // cod ...

随机推荐

  1. main函数的参数

    一.main的参数 形式:int main(int argc,char *argv[]) 参数argc.argv可以被看做是main函数的形参,argc是整型变量,代表的是参数的个数:argv是指向字 ...

  2. reverse string | leetcode

    思路:在原来的字符串后面添加上strlen-1个字符,返回 class Solution { public: string reverseString(string s) { unsigned int ...

  3. <meta http-equiv="pragma" content="no-cache"/>-备

    <meta http-equiv="pragma" content="no-cache"/> 网页中常常看见有这样的标记,他们是清浏览器缓存用的啊, ...

  4. ByteArrayInputStream 和 ByteArrayOutputStream

    package java.io; /** * A <code>ByteArrayInputStream</code> contains * an internal buffer ...

  5. JDK源码阅读(二) AbstractList

    package java.util; public abstract class AbstractList<E> extends AbstractCollection<E> i ...

  6. Kernel Bypass & Offload 介绍

    系统网络优化可以有两方面的工作可以做:1 绕开内核(bypass):2 用硬件替代软件(offload). 具体包括: 1. 绕开内核: 不使用内核内核子系统的功能,采用自己实现的相同功能的代码来处理 ...

  7. spoj 4487. Can you answer these queries VI (gss6) splay 常数优化

    4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...

  8. bzoj 1034: [ZJOI2008]泡泡堂BNB 貪心

    1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1398  Solved: 727[Submit][Sta ...

  9. 在debug模式下运行不报错,换到release模式下报找不到某某库或文件的错。。解决办法

    我遇到的问题是:把edit secheme调到debug模式运行没有问题,然后调到release模式的时候报目录下没有libTuyoo.a 解决办法 把断开真机设备,用IOS device下relea ...

  10. E: Write error - write (28 No space left on device)

    1:在终端中运行cd命令,提示: e: Write error - write (28 No space left on device) E: Cant mmap an empty file 2:使用 ...