可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间。例如:
void elapsed_time()
{
printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);
}
请问为什么要除 CLOCKS_PER_SEC?
因为clock()是以豪秒为单位,要把它换成秒 。
#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");
}
/*
Name is :time_class.h
功能: 用户操作时间日期
作者:张树林 Author:woods.zhang
日期:2004-12-15 Date:2004-12-15
版本:1.0.0 Version:1.0.0
Email:hoojar@163.com QQ:37894354
MSN:hoojar@hotmail.com
*/
#ifndef TIME_CLASS_H_
#define TIME_CLASS_H_
#include <time.h>;
#include <string>;
using namespace std;
class time_class
{
public:
string get_now(bool is_cn = false)
//___________获取当前系统时间______________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
if (is_cn == true)
//___________以中国汉字显示_____________
{
sprintf(str_tmp,"%d年%d月%d日 %d时%d分%d秒",ptm->;tm_year + 1900, ptm->;tm_mon +1 , ptm->;tm_mday, ptm->;tm_hour, ptm->;tm_min, ptm->;tm_sec);
return str_tmp;
}
else
//_________以时间日期方式显示___________
{
sprintf(str_tmp,"%d-%d-%d %d:%d:%d",ptm->;tm_year + 1900, ptm->;tm_mon + 1, ptm->;tm_mday, ptm->;tm_hour, ptm->;tm_min, ptm->;tm_sec);
return str_tmp;
}
}
string get_date(bool is_cn = false)
//____________获取系统日期__________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
if (is_cn == true)
//___________以中国汉字显示_____________
{
sprintf(str_tmp,"%d年%d月%d日", ptm->;tm_year + 1900, ptm->;tm_mon +1 , ptm->;tm_mday);
return str_tmp;
}
else
//_________以时间日期方式显示___________
{
sprintf(str_tmp,"%d-%d-%d", ptm->;tm_year + 1900, ptm->;tm_mon + 1, ptm->;tm_mday);
return str_tmp;
}
}
/*------------------------------------------------------------------------------------------------*/
int get_year()
//___________获取当前年份___________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_year + 1900;
}
/*------------------------------------------------------------------------------------------------*/
int get_month()
//___________获取当前月份___________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_mon + 1;
}
/*------------------------------------------------------------------------------------------------*/
int get_day()
//___________获取当前日份___________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_mday;
}
/*------------------------------------------------------------------------------------------------*/
int get_hour()
//___________获取当前时份___________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_hour;
}
/*------------------------------------------------------------------------------------------------*/
int get_minute()
//___________获取当前分钟___________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_min;
}
/*------------------------------------------------------------------------------------------------*/
int get_second()
//___________获取当前秒____________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_sec;
}
/*------------------------------------------------------------------------------------------------*/
int get_day_in_year()
//___________获取当前是一年当中的第几天___________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_yday;
}
/*------------------------------------------------------------------------------------------------*/
int get_week()
//___________获取当前是周几________数字____________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return ptm->;tm_wday;
}
/*------------------------------------------------------------------------------------------------*/
string get_week(bool is_cn)
//___________获取当前是周几________字母___________
{
string week_cn[7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
string week[7] = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
if (is_cn == true)
{
return week_cn[ptm->;tm_wday];
}
else
{
return week[ptm->;tm_wday];
}
}
/*------------------------------------------------------------------------------------------------*/
int get_how_week()
//_______________获取本周是一年当中第几周____________________
{
time_t rawtime;
struct tm* ptm;
char str_tmp[30];
time (&rawtime);
ptm = localtime(&rawtime);
return int(ptm->;tm_yday / 7) + 1;
}
long get_time_second()
//___________获取到目前为此的秒数________________
{
string tmp_str;
char tmstr[15];
sprintf(tmstr, "%dl", time(NULL));
tmp_str = tmstr;
tmp_str = tmp_str.substr(0,10);
return atol(tmp_str.c_str()); //转换成双精度
}
};
#endif
/*
Name is :time.cpp
编译指令:g++ -o time time.cpp
时间处理函数
作者:张树林
日期:2004-11-22
*/
#include <iostream>;
#include "./time_class.h"
int main ()
{
time_class tc;
cout << "系统