linux系统中的时间

时间:2023-03-09 02:15:32
linux系统中的时间

1.编程显示系统时间:

 #include <stdio.h>
#include <time.h>
/*
gcc -o fix fixedFormatTime.c
./fix
*/
int main()
{
time_t time_raw_format;
time( &time_raw_format); // Get the current time
printf( "Time is [%ld].\n", (long)time_raw_format );///显示从1970年1月1日0时到现在的秒数-64位整数
// Convert the integer time to the fixed-format string
printf( "The current local time is: %s\n", ctime(&time_raw_format) );//显示字符串形式的时间,具体到秒
return ;
}

2.linux的时间测量:

 #include <stdio.h>
#include <math.h>
#include <sys/time.h>
/*
gcc statTime.c -o s -lm
gcc statTime.c -o s(.exe文件名称) -lm(加库说明)
./s
*/
void testFun()
{
unsigned int i,j;
double y;
for(i=; i<; i++ )
{
for(j=; j<; j++ )
{
y = sin((double)i); //time-consuming operation
}
}
}
int main()
{
struct timeval tpstart, tpend;
float timeused; gettimeofday( &tpstart, NULL ); //record the start timestamp
testFun();
gettimeofday( &tpend, NULL ); //record the end timestamp
// compute the used time
timeused = * ( tpend.tv_sec-tpstart.tv_sec) + (tpend.tv_usec-tpstart.tv_usec);
timeused /= ;
printf( "Used Time: %f\n", timeused );
return ;
}

3.linux中的计时器(还不明白咋做的)

 #include <stdio.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
// gcc -o t testtimer.c
void print_info(int signo)
{
printf("SIGPROF Timer fired\n"); //简单的打印,表示 timer 到期
} void init_sigaction(void)
{
struct sigaction act;
act.sa_handler = print_info; //为什么参数可以没有
act.sa_flags = ;
sigemptyset(&act.sa_mask);
sigaction(SIGPROF,&act,NULL); //设置信号 SIGPROF 的处理函数为 print_info
}
void init_time()
{
struct itimerval value;
value.it_value.tv_sec=;
value.it_value.tv_usec=;
value.it_interval=value.it_value;
setitimer(ITIMER_PROF,&value,NULL); //初始化 timer,到期发送 SIGPROF 信号
}
int main()
{
init_sigaction();
init_time();
int sum = ;
// while(1){sum++;if(sum == 10) break;}
while();
return ;
}