Department of Scientific Computing   
Institute for Numerical Simulation   
University of Bonn   
Documentation
Download
Programming References
Bug Reports / Suggestions
FAQ
Authors
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

timer.cc

00001 /*====================================================================
00002  * timer.c :
00003  *=========
00004  * 
00005  * defines functions : diffrusagetime,sumrusagetime,writerusagesystemtime,
00006  *                     writerusageusertime
00007  *
00008  * author : Marc Alexander Schweitzer <schweitz@iam.uni-bonn.de>
00009  *====================================================================*/
00010 #ifdef __RUSAGE_TIME__
00011 
00012 #include <stdio.h>
00013 #include <sys/resource.h>
00014 #include "timer.h"
00015 //#include "error.h"
00016 
00017 struct rusage diffrusagetime(struct rusage stop,struct rusage start)
00018   /* compute difference of times within the rusage structures stop, start
00019    *======================================================================
00020    * stop,start : the rusage structures
00021    *
00022    * return (struct rusage) : struct includes only usefull information in 
00023    *                          the corresponding time elements
00024    *======================================================================*/
00025 {
00026   struct rusage diff;
00027   /* initalize diff */
00028   getrusage (RUSAGE_SELF,&diff);
00029   /* compute time difference */
00030   diff.ru_utime.tv_sec = stop.ru_utime.tv_sec-start.ru_utime.tv_sec;
00031   diff.ru_utime.tv_usec = stop.ru_utime.tv_usec-start.ru_utime.tv_usec;
00032   if (diff.ru_utime.tv_usec<0)
00033     {
00034       --diff.ru_utime.tv_sec;
00035       diff.ru_utime.tv_usec = 1000000+diff.ru_utime.tv_usec;
00036     }
00037   diff.ru_stime.tv_sec = stop.ru_stime.tv_sec-start.ru_stime.tv_sec;
00038   diff.ru_stime.tv_usec = stop.ru_stime.tv_usec-start.ru_stime.tv_usec;
00039   if (diff.ru_stime.tv_usec<0)
00040     {
00041       --diff.ru_stime.tv_sec;
00042       diff.ru_stime.tv_usec = 1000000+diff.ru_stime.tv_usec;
00043     }
00044   return diff;
00045 }
00046 
00047 struct rusage sumrusagetime(struct rusage stop,struct rusage start)
00048   /* compute sum of times within the rusage structures stop, start
00049    *======================================================================
00050    * stop,start : the rusage structures
00051    *
00052    * return (struct rusage) : struct includes only usefull information in 
00053    *                          the corresponding time elements
00054    *======================================================================*/
00055 {
00056   struct rusage sum;
00057   sum.ru_utime.tv_sec = stop.ru_utime.tv_sec+start.ru_utime.tv_sec;
00058   sum.ru_utime.tv_usec = stop.ru_utime.tv_usec+start.ru_utime.tv_usec;
00059   if (sum.ru_utime.tv_usec>1000000)
00060     {
00061       ++sum.ru_utime.tv_sec;
00062       sum.ru_utime.tv_usec = sum.ru_utime.tv_usec-1000000;
00063     }
00064   sum.ru_stime.tv_sec = stop.ru_stime.tv_sec+start.ru_stime.tv_sec;
00065   sum.ru_stime.tv_usec = stop.ru_stime.tv_usec+start.ru_stime.tv_usec;
00066   if (sum.ru_stime.tv_usec>1000000)
00067     {
00068       ++sum.ru_stime.tv_sec;
00069       sum.ru_stime.tv_usec = sum.ru_stime.tv_usec-1000000;
00070     }
00071   return sum;
00072 }
00073 
00074 void writerusagesystemtime(FILE *file,struct rusage time)
00075   /* write the system time of 'time' to 'file'
00076    *==================================================================
00077    * file : the file handle
00078    * time : the struct, which system time data element is to be written
00079    *==================================================================*/
00080 {
00081   double system = (double)time.ru_stime.tv_sec+1e-6*(double)time.ru_stime.tv_usec;
00082   fprintf(file,"%.6f sec",system);
00083 }
00084 
00085 void writerusageusertime(FILE *file,struct rusage time)
00086   /* write the user time of 'time' to 'file'
00087    *==================================================================
00088    * file : the file handle
00089    * time : the struct, which user time data element is to be written
00090    *==================================================================*/
00091 {
00092   double user = (double)time.ru_utime.tv_sec+1e-6*(double)time.ru_utime.tv_usec;
00093   fprintf(file,"%.6f sec",user);
00094 }
00095 
00096 void writerusagetime(FILE *file,struct rusage time)
00097   /* write the user time of 'time' to 'file'
00098    *==================================================================
00099    * file : the file handle
00100    * time : the struct, which system and user time data elements are to be written
00101    *==================================================================*/
00102 {
00103   double user = (double)time.ru_utime.tv_sec+1e-6*(double)time.ru_utime.tv_usec;
00104   double system = (double)time.ru_stime.tv_sec+1e-6*(double)time.ru_stime.tv_usec;
00105   fprintf(file,"system : ");
00106   fprintf(file,"%.6f sec",system);
00107   fprintf(file,"\tuser : ");
00108   fprintf(file,"%.6f sec",user);
00109   fprintf(file,"\tsum : ");
00110   fprintf(file,"%.6f sec",user+system);
00111 }
00112 
00113 
00114 void writerusagetime2(char *strg , struct rusage time)
00115   /* write the user time of 'time' to 'file'
00116    *==================================================================
00117    * file : the file handle
00118    * time : the struct, which system and user time data elements are to be written
00119    *==================================================================*/
00120 {
00121   double user = (double)time.ru_utime.tv_sec+1e-6*(double)time.ru_utime.tv_usec;
00122   double system = (double)time.ru_stime.tv_sec+1e-6*(double)time.ru_stime.tv_usec;
00123   printf("%s system : ",strg);
00124   printf("%.6f sec",system);
00125   printf("\tuser : ");
00126   printf("%.6f sec",user);
00127   printf("\tsum : ");
00128   printf("%.6f sec",user+system);
00129 }
00130 
00131 
00132 
00133 #else
00134 #include <time.h>
00135 #include <stdio.h>
00136 #include "timer.h"
00137 
00138 #endif

Generated at Mon Aug 19 10:02:32 2002 for AWFD by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001