C ++中的difftime()函数
在本文中,我们将讨论difftime()C++中的函数,其语法,工作原理和返回值。
difftime()函数是C++中的内置函数,在头文件中定义。函数接受time_t类型的两个参数,函数计算两次之间的差
语法
double difftime(time_t end, time_t beginning);
返回值
返回以double数据类型存储的时间差(以秒为单位)。
示例
#include <stdio.h>
#include <time.h>
int main () {
time_t now;
struct tm newyear;
double seconds;
time(&now); /* get current time; */
newyear = *localtime(&now);
newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;
newyear.tm_mon = 0; newyear.tm_mday = 1;
seconds = difftime(now,mktime(&newyear));
printf ("%.f seconds since new year in the current timezone.\n", seconds);
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
3351041 seconds since new year in the current timezone.
示例
#include <iostream>
#include <ctime>
using namespace std;
int main() {
time_t start, ending;
long addition;
time(&start);
for (int i = 0;
i < 50000; i++) {
for (int j = 0; j < 50000; j++);
}
for (int i = 0; i < 50000; i++) {
for (int j = 0; j < 50000; j++);
} for (int i = 0; i < 50000; i++) {
for (int j = 0; j < 50000; j++);
} time(&ending);
cout << "Total time required = " << difftime(ending, start) << " seconds " << endl;
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
Total time required = 37 seconds
热门推荐
2 训练狗狗成为
3 训练狗狗几岁
4 反应训练狗狗
5 听力训练狗狗
7 狗狗尿训练
8 拳击训练狗狗
10 狗狗训练卧室
11 一个月狗狗怎么训练
12 几个月训练狗狗大小便
13 牧羊犬训练狗狗
14 狗狗训练做
15 狗狗训练它
16 可以训练狗狗
17 训练狗狗给球
18 训练狗狗的技巧