C 库函数

&C语言风格

#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

int main ()
{
	time_t start_t, end_t;
	double diff_t;

	printf("程序启动...
");
	time(&start_t);

	printf("休眠 5 秒...
");
	Sleep(5000);    //5000ms

	time(&end_t);
	diff_t = difftime(end_t, start_t);

	printf("执行时间 = %f
", diff_t);
	printf("程序退出...
");


	system("pause");
	return(0);
}

  

&C++风格

#include <iostream>
#include <ctime>
#include <Windows.h>

using namespace std;

//C 库函数 double difftime(time_t time1, time_t time2) 返回 time1 和 time2 之间相差的秒数 (time1 - time2)。
//这两个时间是在日历时间中指定的,表示了自纪元 Epoch(协调世界时 UTC:1970-01-01 00:00:00)起经过的时间
int main()
{
    time_t start_t, end_t;
    time(&start_t);
    Sleep(5000);     //Sleep为Windows.h的函数
    time(&end_t);
    cout << start_t << endl;
    cout << end_t << endl;
    cout << difftime(end_t, start_t) << endl;
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/gohikings/p/9850292.html