c语言time函数怎么使用

In this article, we’ll take a look at using the time() function in C/C++.

在本文中,我们将研究在C / C ++中使用time()函数。

The time() function is a useful utility function that we can use to measure the elapsed time of our program.

time()函数是一个有用的实用程序函数,可用于测量程序的经过时间。

Let’s look at how we can use this function, using some simple examples!

让我们来看一些简单的例子,看看如何使用此功能!



C / C ++中time()函数的基本语法 (Basic Syntax of the time() function in C/C++)

This function belongs to the header file <time.h>, so we must include this before calling time().

该函数属于头文件<time.h> ,因此在调用time()之前必须包含此文件。


#include <time.h>
time_t time(time_t *tloc);

This takes in a pointer to a time_t datatype and returns the time elapsed since 00:00:00 UTC, January 1, 1970.

这将获取一个指向time_t数据类型的指针,并返回从1970年1月1日UTC 00:00:00开始经过的时间。

The time value returned is in seconds, so you must make suitable conversions from it.

返回的时间值以秒为单位,因此您必须从中进行适当的转换。

If tloc is not a null pointer, the returned value is also stored in the object pointed to by second.

如果tloc 不是空指针,则返回的值也存储在second指向的对象中。

Otherwise, if tloc is NULL, the return value is not stored anywhere.

否则,如果tlocNULL ,则返回值不会存储在任何地方。

Let’s look at some simple examples now.

现在让我们看一些简单的例子。

在C / C ++中使用time()–一些示例 (Using time() in C/C++ – Some Examples)

Let’s first look at the case when tloc is a NULL pointer.

首先让我们看一下tloc是NULL指针的情况。


#include <stdio.h>
#include <time.h>int main() {time_t num_seconds = time(NULL); // Passing NULL to time()printf("Since 1st January 1970 UTC, No. of seconds = %d, No. of days = %d", (int)num_seconds, (int)num_seconds/86400);return 0;
}

Output

输出量


Since 1st January 1970 UTC, No. of seconds = 1592317455, No. of days = 18429

As of the time of writing (June 16 2020), this is indeed true!

截至撰写本文时(2020年6月16日),确实如此!

Now, let’s go to the second case, when tloc is not NULL.

现在,让我们转到第二种情况,即tloc不为NULL时。

In this case, since the return value will be stored to the pointer location, there is no need to assign it separately!

在这种情况下,由于返回值将存储在指针位置,因此无需单独分配它!


#include <stdio.h>
#include <time.h>int main() {time_t tloc;time(&tloc); // Storing the return value to tlocprintf("Since 1st January 1970 UTC, No. of seconds = %d, No. of days = %d", (int)tloc, (int)tloc/86400);return 0;
}

This will give a similar output as before.

这将提供与以前相似的输出。



结论 (Conclusion)

In this article, we learned about using the time() function in C / C++, to get the time elapsed since the first epoch.

在本文中,我们学习了如何在C / C ++中使用time()函数来获取自第一个时期以来所经过的时间。

For more content on C and C++, do go through our tutorial section on C programming!

有关C和C ++的更多内容,请阅读我们有关C编程的教程部分 !

参考资料 (References)

  • Linux Manual Page on time() in CLinux手册页 on time()in C


翻译自: https://www.journaldev.com/41447/time-function-in-c-plus-plus

c语言time函数怎么使用

c语言time函数怎么使用_如何在C / C ++中使用time()函数?相关推荐

  1. execvp函数详解_如何在C / C ++中使用execvp()函数

    execvp函数详解 In this article, we'll take a look at using the execvp() function in C / C++. 在本文中,我们将介绍如 ...

  2. python3 lambda函数字典排序_排序字典表理解中的lambda函数

    我有以下词典:student_loan_portfolio = { 'loan1': {'rate': .078, 'balance': 1000, 'payment': 100, 'prepayme ...

  3. python 中split函数的应用_举例详解Python中的split()函数的使用方法

    函数:split() python中有split()和os.path.split()两个函数,具体作用如下: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(li ...

  4. python类中函数_如何在Python类中使用模块函数

    参见英文答案 > How do you call a private module function from inside a class?                           ...

  5. R语言广义线性模型函数GLM、R中有几种logistic回归扩展和变异、robust包中的glmRob函数鲁棒logistic回归、ms包中的lrm函数拟合序数逻辑回归

    R语言广义线性模型函数GLM.glm函数构建逻辑回归模型(Logistic regression).R中有几种logistic回归扩展和变异.robust包中的glmRob函数鲁棒logistic回归 ...

  6. php变量在html调用函数调用,PHP_如何在html标记中调用的函数里传递对象,最近使用jquery结合Ajax开发一个 - phpStudy...

    如何在html标记中调用的函数里传递对象 最近使用jquery结合Ajax开发一个中小型网站应用.在后台管理中要使用Ajax调用系统功能,根据Ajax的返回结果,使用jQuery对前台页面进行操作. ...

  7. c语言 lookup函数怎么用_菜鸟记502用lookup和vlookup函数核对数据的一个小栗子

    欢迎转发扩散点在看 万一您身边的朋友用得着呢? 各位朋友早上好,小菜继续和您分享经验之谈,截止今日小菜已分享400+篇经验之谈,可以文章编号或关键词进行搜索. 微信推送规则发生改变,如果您想看到小菜每 ...

  8. python使用函数的目的_在Python 3.x中经常看到定义函数有一个单独的 * 参数?定义这样参数的目的是?怎样对其取值呢?...

    参数在python中总是通过赋值进行传递的.在默认情况下,参数是通过其位置进行匹配的,从左到右,而且必须精确的传递和函数头部参数名一样多的参数. 这种默认的传递方式很简单 def f(a,b,c): ...

  9. c语言如何输入矩阵_如何在 COMSOL 软件中调试外部材料

    问题描述 我实现了一种用于"结构力学"的外部材料,但我的模型无法求解. 解决方法 外部材料一般通过 C 语言编码,然后被编译到共享库中,供 COMSOL Multiphysics ...

最新文章

  1. 分享Silverlight/WPF/Windows Phone一周学习导读(3月14日-3月19日)
  2. Linux多台服务器共用密钥ssh自动登陆
  3. [SDOI2017]硬币游戏
  4. Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表
  5. Ajax+Python flask实现上传文件功能
  6. 爬虫篇 --- 分布式爬虫
  7. 理性预期学派(Rational Expectation School)
  8. 云炬Android开发笔记 6启动图功能开发与封装
  9. 实践2.4 ELF文件格式分析
  10. android 音乐资源获取失败,android – 获取嵌入式mp3文件嵌入式艺术失败
  11. 利用openpyxl模块来操作Excel
  12. 如何让同局域网的同事访问我电脑上的PHP网站和数据库
  13. 使用JavaMail发送普通邮件(纯文本邮件)
  14. mysql下载速度慢怎么办
  15. 百度AI 开放平台 - 各大AI 开放平台开放能力解读和分析,技术的应用场景,技术前景
  16. HDMI协议解析-从软硬件角度分析
  17. 模式窗口window.showModalDialog()的用法
  18. 固态硬盘能提高服务器内存吗,内存涨疯了!固态硬盘能替代内存吗?
  19. 解读-m addrtype --dst-type LOCAL -j DOCKER
  20. 常用软件的历史版本资源下载汇总(持续更新)

热门文章

  1. Xcode历史版本下载
  2. FPD LINK III的同轴POC电路布局布线
  3. 賣鞋網站MODISTA
  4. 如何在Mac上查看隐藏文件,这四个方法百试不爽!
  5. thinkphp5迁移的时候,数据库连接老是错误 THINKPHP SQLSTATE[HY000] [1045]
  6. 入门学习MySQL数据库基础
  7. 【JY】建筑结构施加地震波的方法与理论机理
  8. 入门篇笔记二(字迹潦草也请莫见怪)
  9. Missing Windows Desktop Extension SDK for UWP
  10. 【Leetcode】1597. Build Binary Expression Tree From Infix Expression