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
热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短