• linux系统中的时间


    1.编程显示系统时间:

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

    2.linux的时间测量:

     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <sys/time.h>
     4 /*
     5 gcc statTime.c -o s -lm
     6 gcc statTime.c -o s(.exe文件名称) -lm(加库说明)
     7 ./s
     8 */
     9 void testFun()
    10 {
    11     unsigned int i,j;
    12     double y;
    13     for(i=0; i<10000; i++ )
    14     {
    15         for(j=0; j<10000; j++ )
    16         {
    17             y = sin((double)i);  //time-consuming operation
    18         }
    19     }
    20 }
    21 int main()
    22 {
    23     struct timeval tpstart, tpend;
    24     float  timeused;
    25     
    26     gettimeofday( &tpstart, NULL ); //record the start timestamp
    27     testFun();
    28     gettimeofday( &tpend, NULL );   //record the end timestamp
    29     // compute the used time
    30     timeused = 1000000 * ( tpend.tv_sec-tpstart.tv_sec) + (tpend.tv_usec-tpstart.tv_usec);
    31     timeused /= 1000000;
    32     printf( "Used Time: %f
    ", timeused );
    33     return 0;
    34 }

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

     1 #include <stdio.h>
     2 #include <time.h>
     3 #include <signal.h>
     4 #include <sys/time.h>
     5 // gcc -o t testtimer.c
     6 void print_info(int signo) 
     7 { 
     8  printf("SIGPROF Timer fired
    "); //简单的打印,表示 timer 到期
     9 } 
    10 
    11 void init_sigaction(void) 
    12 { 
    13  struct sigaction act;
    14  act.sa_handler = print_info; //为什么参数可以没有
    15  act.sa_flags = 0; 
    16  sigemptyset(&act.sa_mask); 
    17  sigaction(SIGPROF,&act,NULL); //设置信号 SIGPROF 的处理函数为 print_info
    18 } 
    19 void init_time() 
    20 { 
    21  struct itimerval value; 
    22  value.it_value.tv_sec=2; 
    23  value.it_value.tv_usec=0; 
    24  value.it_interval=value.it_value; 
    25  setitimer(ITIMER_PROF,&value,NULL); //初始化 timer,到期发送 SIGPROF 信号
    26 } 
    27 int main() 
    28 { 
    29  init_sigaction(); 
    30  init_time(); 
    31  int sum = 0;
    32 // while(1){sum++;if(sum == 10) break;}
    33 while(1);
    34  return 0;
    35 }
  • 相关阅读:
    用C++读写EXCEL文件的几种方式比较
    20个值得收藏的网页设计开放课件
    char* 应用, 去除字符串内多余空格, 用算法而非库函数
    东拉西扯:王建硕主义
    Lisp 的本质(The Nature of Lisp)
    web前端:html
    [原译]理解并实现原型模式实现ICloneable接口.理解深浅拷贝
    [原译]理解并实现装饰器模式
    3分钟理解Lambda表达式
    [原译]实现IEnumerable接口&理解yield关键字
  • 原文地址:https://www.cnblogs.com/ACMERY/p/4950617.html
Copyright © 2020-2023  润新知