c和c++中的时间

//clock()返回程序开始执行后所用的系统时间;
//头文件ctime定义了一个符号常量—CLOCKS_PER_SEC ,该常量等于每秒钟包含的系统时间单位数;
//将系统时间除以CLOCKS_PER_SEC可以得到秒数;将秒数乘以CLOCKS_PER_SEC可以得到以系统时间为单位的时间;
//ctime将clock_t作为clock()返回类型的别名,所以可以将变量声明为 clock_t;
//可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间;
// 可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间;
 
//等待时间
#include <iostream>
#include <ctime>
int main()
{
 using namespace std;
 float  secs;
 cin >>secs;
 clock_t delay =secs* CLOCKS_PER_SEC;
 clock_t start =clock();
 while (clock()-start<delay)
 ;
 cout <<"done!\n" ;
 system("pause");
  
}

// 计算你的机器运行一个循环或者处理其它事件到底花了多少时间
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
long i = 10000000L;
clock_t start, finish;
double duration;
/* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i) ;
start = clock();
while( i-- );
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", duration );
system("pause");
}

// ctime函数使用
// 功 能: 把日期和时间转换为字符串
 #include <stdio.h>
#include <time.h>
int main(void)
{
time_t t;
t=time(&t);
printf("Today's date and time: %s\n", ctime(&t)); //得到日期和时间
system("pause");
return 0;
}

//得到从 1970年1月1日午夜 到现在的秒数;
#include <stdio.h>
#include <time.h>
int main()
{
 time_t t;
 time(&t);
 printf("%d",t);  
 system("pause");
}

你可能感兴趣的:(time.h)