我们运行程序的时候,可以简单使用clock函数测试程序的运行时间:(本示例中以微秒为单位输出)

https://github.com/yaowenxu/Workplace/blob/master/timer/clocktimer.c

/**
* Author: Yaowen Xu
* Github: https://github.com/yaowenxu
* Organization: 北航系统结构研究所
* Date: 2019-08-18 11:59:54
* LastEditTime: 2019-08-18 12:45:45
* Description: 使用 C 语言标准库函数clock来进行测试程序运行时间
*/
#include <time.h>
#include <stdio.h>
#include <math.h> int str2int(char* str){
char *p = str;
int sum = ;
while (*p != '\0')
{
sum = sum* + (*p-'');
p++;
}
return sum;
} int main(int argc, char* argv[]){
clock_t start, stop;
int def = ;
if (argc == )
{
def = str2int(argv[argc-]);
}
start = clock();
for (int i = ; i < def ; i++)
{
float tmp = sqrt(i);
}
stop = clock();
double total = stop - start; // 使用运行的时间
printf("Clocks: %.1f\n", total); // 总共使用的时钟
printf("Time: %.1f us\n", total*1e6/(CLOCKS_PER_SEC)); // 转换运行时间为微秒
return ;
} /*
* Approximates the processor time used by the program,
* since the beginning of an implementation-defined time
* period that is related to the program invocation.
* To measure the time spent in a program, call the clock()
* function at the start of the program, and subtract its
* returned value from the value returned by subsequent calls
* to clock(). Then, to obtain the time in seconds, divide
* the value returned by clock() by CLOCKS_PER_SEC.
*
* If you use the system() function in your program, do not
* rely on clock() for program timing, because calls to system()
* may reset the clock.
*
* In a multithread POSIX C application, if you are creating threads
* with a function that is based on a POSIX.4a draft standard,
* the clock() function is thread-scoped.
*
* Refer:https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/clock.htm
*/

保持更新,更多文章,请关注cnblogs.com/xuyaowen

程序运行时间测试 - 使用libc 中 clock 函数的更多相关文章

  1. 程序运行时间测试 - 使用libc 中 time 函数 实现秒级的运行时间检测

    c 标准库中,有time 函数,可以返回 1970年1月1日 开始到现在的秒数,我们可以调用两次的时间差来计算程序运行时间: https://github.com/yaowenxu/Workplace ...

  2. 程序运行时间测试 - 使用系统函数 getrusage 获取程序运行时间

    https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c 关键结构体: struct rusage { stru ...

  3. 关于控制台程序下使用mfc库中的函数时断言

    例如: TCHAR path[8192]; int len = getmodulefilename(afxgetinstancehandle(),path,8192);//会出现断言 如果没有选择支持 ...

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

    转自:http://blog.csdn.net/ghevinn/article/details/22800059 DWORD start_time=GetTickCount(); {...} DWOR ...

  5. C和C++中函数运行时间测试

    //clock()函数为c中,捕捉从程序开始运行到clock运行的时间//时间单位为clock tick,即为时钟打点#include<iostream>#include<cmath ...

  6. C++中如何记录程序运行时间

    一.clock()计时函数clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:clock_t clock(void) ;简单而言, ...

  7. c中计时函数 clock()

    #include<time.h> int main() { // ... .. // .... printf("Time used = %.2lf\n",(double ...

  8. 不要在Lua中使用os.clock()函数

    1.os.clock函数的实现是调用了c语言的函数函数库,实现代码如下: static int os_clock (lua_State *L) { lua_pushnumber(L, ((lua_Nu ...

  9. 程序代码中退出函数exit()与返回函数return ()的区别

    程序代码中退出函数exit()与返回函数return ()的区别   exit(0):正常运行程序并退出程序:   exit(1):非正常运行导致退出程序:   return():返回函数,若在主函数 ...

随机推荐

  1. layui table 分页 序号始终从”1“开始解决方法

    在用Layui table 分页显示数据,用 type:"numbers" 进行显示序号有以下的问题 1.表格自带的分页,page:true 这种分页,在切换页面的时候序号可以正常 ...

  2. jQuery从零开始(三)-----ajax

    jq当中的ajax技术 $.ajax $.get() $.post() $.load() $.ajax({ url:'请求文件的地址', type:'请求文件使用的方法', data:'向请求的api ...

  3. 【转载】Android内存泄漏的8种可能

    Java是垃圾回收语言的一种,其优点是开发者无需特意管理内存分配,降低了应用由于局部故障(segmentation fault)导致崩溃,同时防止未释放的内存把堆栈(heap)挤爆的可能,所以写出来的 ...

  4. Ride to Office

    [题目描述] 起点与终点相隔4500米.现Charley需要从起点骑车到终点.但是,他有个习惯,沿途需要有人陪伴,即以相同的速度,与另外一个人一起骑.而当他遇到以更快的速度骑车的人时,他会以相应的速度 ...

  5. oracle12.2RAC之OGG安装配置(二)

    本机到本机的配置: 源端       10.1.83.144:1521/SIBP_GSDY    HNSIB_GSDY目标端   10.1.83.144:1521/SIBP_GS         HN ...

  6. Linux—磁盘管理

    https://www.cnblogs.com/new-journey/p/10076387.html https://www.cnblogs.com/jiangxiaoxian/p/9610903. ...

  7. 手工挖掘web常见漏洞时的一些经验总结

    一.前提: 漏洞挖掘原则 所有变量 所有头 cookie中的变量 逐个变量删除 漏洞的本质 数据与指令的混淆 对用户输入信息过滤不严判断失误,误将数据当指令 二.经典漏洞: 1.身份认证 常用弱口令/ ...

  8. 微信小程序支付功能讲解(2)

    小程序支付 业务流程时序图 官方文档 步骤: 1. Openid 在小程序初次加载的时候就已经获取(详情见 小程序登录) 2. 生成商户订单 1.商品信息由小程序端提供 2.提供支付统一下单接口所需参 ...

  9. 一天两道PAT(2)1005,1006

    今天的pat1006没什么好说的.就记录一下1005的状况.先上题目. 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重 ...

  10. WPF 精修篇 page

    原文:WPF 精修篇 page 前言 前段时间看UML 大象 这本书 虽然马上看到了精华片 最后还是暂时暂停 因为这本书 很好 但是暂时对现在的我来说 有点超前 很多东西理解起来还是很难 但是 这本书 ...