程序运行时间测试 - 使用libc 中 clock 函数
我们运行程序的时候,可以简单使用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 函数的更多相关文章
- 程序运行时间测试 - 使用libc 中 time 函数 实现秒级的运行时间检测
		c 标准库中,有time 函数,可以返回 1970年1月1日 开始到现在的秒数,我们可以调用两次的时间差来计算程序运行时间: https://github.com/yaowenxu/Workplace ... 
- 程序运行时间测试 - 使用系统函数 getrusage 获取程序运行时间
		https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c 关键结构体: struct rusage { stru ... 
- 关于控制台程序下使用mfc库中的函数时断言
		例如: TCHAR path[8192]; int len = getmodulefilename(afxgetinstancehandle(),path,8192);//会出现断言 如果没有选择支持 ... 
- 【转】c++ 获取程序运行时间
		转自:http://blog.csdn.net/ghevinn/article/details/22800059 DWORD start_time=GetTickCount(); {...} DWOR ... 
- C和C++中函数运行时间测试
		//clock()函数为c中,捕捉从程序开始运行到clock运行的时间//时间单位为clock tick,即为时钟打点#include<iostream>#include<cmath ... 
- C++中如何记录程序运行时间
		一.clock()计时函数clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:clock_t clock(void) ;简单而言, ... 
- c中计时函数 clock()
		#include<time.h> int main() { // ... .. // .... printf("Time used = %.2lf\n",(double ... 
- 不要在Lua中使用os.clock()函数
		1.os.clock函数的实现是调用了c语言的函数函数库,实现代码如下: static int os_clock (lua_State *L) { lua_pushnumber(L, ((lua_Nu ... 
- 程序代码中退出函数exit()与返回函数return ()的区别
		程序代码中退出函数exit()与返回函数return ()的区别 exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: return():返回函数,若在主函数 ... 
随机推荐
- 剑指offer 27:二叉搜索树与双向链表
			题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 解题思路 采用中序遍历遍历二叉树,利用二叉排序树的特性,顺次连接节点,形成 ... 
- python数据库模块
			安装数据库 [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3/centos7-amd64 gpgkey=https://yu ... 
- centos 6.9 升级glibc动态库
			glibc是gnu发布的libc库,即c运行库,glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc.glibc除了封装linux操作系统所提供的系统服务外,它本身也提供 ... 
- Educational Codeforces Round 71 (Rated for Div. 2)
			传送门 A.There Are Two Types Of Burgers 签到. B.Square Filling 签到 C.Gas Pipeline 每个位置只有"高.低"两种状 ... 
- 使用 github pages快速部署自己的静态网页
			看见很多大神在Github Pages上部署Demo,感觉效果还不错,刚才自己也试了一下,发现其实并不难! 选择 github pages 的理由 使用零成本: github pages 集成在 gi ... 
- mysql关联两张表时的编码问题
			Mysql关联两张表时,产生错误提示Illegal mix of collations 1.先用工具把数据库.两张表的编码方式改变 2.这步很重要,需要改变字段的编码方式. ALTER TABLE ` ... 
- Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟
			C. Queue in the Train There are 
- python画一片绿叶给你
			怎么用 turtle 画一个 π 字,于是我顺手到网上大致搜了下,发现网上没有画这个 π 字的,接着又用谷歌加英文搜索了下,还是没找到现成的答案. 不过通过这次搜索意外发现了一个有趣的网站,网站上有大 ... 
- C语言程序设计100例之(3): Cantor表
			例3 Cantor表 题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 …… 2/1 ... 
- 程序员,你还不会合理选择Filter、Interceptor、Aspect?
			小伙伴们应该听说过过滤器.拦截器.切面,印象上都能够起到截断拦截的作用,在做一些业务需求时,不知道如何选择,今天老顾就来介绍一下他们之间的区别. 过滤器可以拦截到方法的请求和响应 (ServletRe ... 
