linux 上时间相关的函数 time ,_ftime , gettimeofday, clock_gettime

原文链接:http://blog.csdn.net/sunlylorn/article/details/6313278

  1. time()提供了秒级的精确度  
  2.   
  3. 1、头文件 <time.h>  
  4. 2、函数原型  
  5. time_t time(time_t * timer)   
  6. 函数返回从TC1970-1-1 0:0:0开始到现在的秒数  
  7.   
  8. 用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。  
  9.  
  10. #include <time.h>  
  11. #include <stdio.h>  
  12. int main(void)  
  13. {  
  14.     time_t t;   
  15.     t = time(NULL);  
  16.     printf("The number of seconds since January 1, 1970 is %ld",t);  
  17.       
  18.     return 0;  
  19. }   
  20.  
  21. #include <stdio.h>  
  22. #include <stddef.h>  
  23. #include <time.h>  
  24. int main(void)  
  25. {  
  26.     time_t timer;//time_t就是long int 类型  
  27.     struct tm *tblock;  
  28.     timer = time(NULL);//这一句也可以改成time(&timer);  
  29.     tblock = localtime(&timer);  
  30.     printf("Local time is: %s/n",asctime(tblock));  
  31.       
  32.     return 0;  

==========================================================================================

  1. _ftime()提供毫秒级的精确度  
  2.   
  3. 1、头文件 <sys/types.h> and <sys/timeb.h>   
  4. 2、函数原型  
  5. void _ftime(struct _timeb *timeptr);   
  6. 参数说明:  
  7.     struct _timeb   
  8.     {  
  9.         time_t time;  
  10.         unsigned short millitm;  
  11.         short timezone;  
  12.         short dstflag;  
  13.     };   
  14.  
  15. #include <stdio.h>  
  16. #include <sys/timeb.h>  
  17. #include <time.h>  
  18.   
  19. void main( void )  
  20. {  
  21.     struct _timeb timebuffer;  
  22.     char *timeline;  
  23.   
  24.     _ftime( &timebuffer );  
  25.     timeline = ctime( & ( timebuffer.time ) );  
  26.   
  27.     printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );  

=======================================================================================

  1.  gettimeofday()提供了微秒级的精确度
  2. 1、头文件 <time.h>  
  3. 2、函数原型  
  4. int gettimeofday(struct timeval *tv, struct timezone *tz);   
  5.   
  6. gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中(可用NULL)。  
  7. 参数说明:  
  8.     timeval结构定义为:  
  9.     struct timeval  
  10.     {  
  11.         long tv_sec; /*秒*/  
  12.         long tv_usec; /*微秒*/  
  13.     };  
  14.     timezone 结构定义为:  
  15.     struct timezone  
  16.     {  
  17.         int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/  
  18.         int tz_dsttime; /*日光节约时间的状态*/  
  19.     };  
  20.     上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下  
  21.         DST_NONE /*不使用*/  
  22.         DST_USA /*美国*/  
  23.         DST_AUST /*澳洲*/  
  24.         DST_WET /*西欧*/  
  25.         DST_MET /*中欧*/  
  26.         DST_EET /*东欧*/  
  27.         DST_CAN /*加拿大*/  
  28.         DST_GB /*大不列颠*/  
  29.         DST_RUM /*罗马尼亚*/  
  30.         DST_TUR /*土耳其*/  
  31.         DST_AUSTALT /*澳洲(1986年以后)*/  
  32.    
  33. 返回值: 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。  
  34.  
  35. #include<stdio.h>  
  36. #include<time.h>  
  37. int main(void)  
  38. {  
  39.     struct timeval tv;  
  40.     struct timezone tz;  
  41.       
  42.     gettimeofday (&tv , &tz);  
  43.       
  44.     printf(“tv_sec; %d/n”, tv,.tv_sec) ;  
  45.     printf(“tv_usec; %d/n”,tv.tv_usec);  
  46.       
  47.     printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest);  
  48.     printf(“tz_dsttime, %d/n”,tz.tz_dsttime);  
  49.       
  50.     return 0;  
  51. }

=======================================================================================

  1. clock_gettime( ) 提供了纳秒级的精确度  
  2.   
  3. 1、头文件 <time.h>  
  4. 2、编译&链接。在编译链接时需加上 -lrt ;因为在librt中实现了clock_gettime函数  
  5. 3、函数原型  
  6. int clock_gettime(clockid_t clk_id, struct timespect *tp);  
  7.     参数说明:  
  8.     clockid_t clk_id 用于指定计时时钟的类型,有以下4种:  
  9.         CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变  
  10.         CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响  
  11.         CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间  
  12.         CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间  
  13.     struct timespect *tp用来存储当前的时间,其结构如下:  
  14.         struct timespec  
  15.         {  
  16.             time_t tv_sec; /* seconds */  
  17.             long tv_nsec; /* nanoseconds */  
  18.         };  
  19.     返回值。0成功,-1失败  
  20.  
  21. #include<stdio.h>  
  22. #include<time.h>  
  23. int main()  
  24. {  
  25.     struct timespec ts;  
  26.       
  27.     clock_gettime(CLOCK_REALTIME, &ts);  
  28.     printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec);  
  29.       
  30.     clock_gettime(CLOCK_MONOTONIC, &ts);//打印出来的时间跟 cat /proc/uptime 第一个参数一样  
  31.     printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec);  
  32.       
  33.     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);  
  34.     printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);  
  35.       
  36.     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);  
  37.     printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);  
  38.       
  39.     printf("/n%d/n", time(NULL));  
  40.   
  41.     return 0;  
  42. }  
  43. /proc/uptime里面的两个数字分别表示:   
  44. the uptime of the system (seconds), and the amount of time spent in idle process (seconds).   
  45. 把第一个数读出来,那就是从系统启动至今的时间,单位是秒 


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
base_efron <- function(y_test, y_test_pred) { time = y_test[,1] event = y_test[,2] y_pred = y_test_pred n = length(time) sort_index = order(time, decreasing = F) time = time[sort_index] event = event[sort_index] y_pred = y_pred[sort_index] time_event = time * event unique_ftime = unique(time[event!=0]) m = length(unique_ftime) tie_count = as.numeric(table(time[event!=0])) ind_matrix = matrix(rep(time, times = length(time)), ncol = length(time)) - t(matrix(rep(time, times = length(time)), ncol = length(time))) ind_matrix = (ind_matrix == 0) ind_matrix[ind_matrix == TRUE] = 1 time_count = as.numeric(cumsum(table(time))) ind_matrix = ind_matrix[time_count,] tie_haz = exp(y_pred) * event tie_haz = ind_matrix %*% matrix(tie_haz, ncol = 1) event_index = which(tie_haz!=0) tie_haz = tie_haz[event_index,] cum_haz = (ind_matrix %*% matrix(exp(y_pred), ncol = 1)) cum_haz = rev(cumsum(rev(cum_haz))) cum_haz = cum_haz[event_index] base_haz = c() j = 1 while(j < m+1) { l = tie_count[j] J = seq(from = 0, to = l-1, length.out = l)/l Dm = cum_haz[j] - J*tie_haz[j] Dm = 1/Dm Dm = sum(Dm) base_haz = c(base_haz, Dm) j = j+1 } base_haz = cumsum(base_haz) base_haz_all = unlist( sapply(time, function(x){ if else( sum(unique_ftime <= x) == 0, 0, base_haz[ unique_ftime==max(unique_ftime[which(unique_ftime <= x)])])}), use.names = F) if (length(base_haz_all) < length(time)) { base_haz_all <- c(rep(0, length(time) - length(base_haz_all)), base_haz_all) } return(list(cumhazard = unique(data.frame(hazard=base_haz_all, time = time)), survival = unique(data.frame(surv=exp(-base_haz_all), time = time)))) }改成python代码
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值