程序运行时间测试 - 使用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():返回函数,若在主函数 ...
随机推荐
- scrapy实例:爬取天气、气温等
1.创建项目 scrapy startproject weather # weather是项目名称 scrapy crawl spidername开始运行,程序自动使用start_urls构造Requ ...
- JS中的slice()和splice()的区别以及记忆方式
总结 splice()会改变原来的数组,返回的是被改变的内容,比如说通过splice删掉了某一项,那么返回的是删掉的这一项,当然还是会以数组的形式返回. slice不会对原数组进行改变,会返回一个新的 ...
- Microsoft Visual Studio 2017 找不到 Visual Studio Installer
Microsoft Visual Studio 2017 找不到 Visual Studio Installer ? 打开vs2017 ,选择 工具 --> 扩展和更新 --> 联机,搜索 ...
- latex初步入门:springer llncs
最近写一篇论文呢,使用到了latex,记录一下吧. 使用的是标准 : springer 的llncs类 核心:使用官方的samplepaper.tex基础上进行修改 这个是最快上手,也是最快能码出一篇 ...
- 使用Settings sync同步VS Code配置
使用Settings sync同步VS Code配置 因为要在多台电脑上使用VSCode,想要简单地管理VSCode地配置,不用每次手动去一一配置,保持多个开发环境的同步,于是使用Settings s ...
- 微信退款异步通知报错Illegal key size or default parameters 的解决办法
问题原因: Java几乎各种常用加密算法都能找到对应的实现.因为美国的出口限制,Sun通过权限文件(local_policy.jar.US_export_policy.jar)做了相应限制.因此存在一 ...
- fastadmin中js是如何调用的
想要了解fastadmin中的js是怎么调用的,就应该先了解RequireJs. RequireJs是模块化工具,每一个我们自己的js文件或者库都可以看成是一个模块,按需引入.写法如下: <sc ...
- go语言设计模式之state
state.go package main import ( "fmt" "math/rand" "os" "time" ...
- Java Web 学习(9) —— EL 与 JSTL
EL 与 JSTL EL与JSTL的作用是为了减少JSP页面中的代码. EL EL(Expression Language):表达式语言 常用于取值 语法 EL 表达式以${开头,以}结束. 多个表达 ...
- Node.js使用Nodemailer发送邮件
除了Python,在node中收发电子邮件也非常简单,因为强大的社区有各种各样的包可以供我么直接使用.Nodemailer包就可以帮助我们快速实现发送邮件的功能. Nodemailer简介 Nodem ...