home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / rtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  3.7 KB  |  113 lines

  1. /**********************************************************************/
  2. /* rtime.c :                                                          */
  3. /*                                                                    */
  4. /* Program timing routines                                            */
  5. /*                                                                    */
  6. /* Copyright (C) 1992, Bernard Kwok                                   */
  7. /* All rights reserved.                                               */
  8. /* Revision 1.0                                                       */
  9. /* May, 1992                                                          */
  10. /**********************************************************************/
  11. #include <stdio.h>
  12. #include <sys/time.h>
  13. #include <sys/resource.h>
  14. #include "rtime.h"
  15. #include "io.h"
  16.  
  17. extern OptionType Option;
  18. extern FILE *rlogfile;
  19. Time_Stats tstats;
  20.  
  21. /**********************************************************************/
  22. /* Get current user and system time                                   */
  23. /**********************************************************************/
  24. float Cpu_Time(utime, stime)
  25.      float *utime, *stime;
  26. {
  27.   struct rusage usage;
  28.   
  29.   if (!getrusage(RUSAGE_SELF, &usage)) {
  30.     *utime = (float) usage.ru_utime.tv_sec + 
  31.       (float) usage.ru_utime.tv_usec / 1000000.0;
  32.     *stime = (float) usage.ru_stime.tv_sec + 
  33.       (float) usage.ru_stime.tv_usec / 1000000.0;
  34.   } else {
  35.     fprintf(stderr,"GetTime: can't get time using getrusage\n");
  36.     exit(1);
  37.   }
  38.   return (*utime + *stime);
  39. }  
  40.  
  41. /**********************************************************************/
  42. /* Return time elapsed since last check                               */
  43. /**********************************************************************/
  44. float Elapsed_Time()
  45. {
  46.   float elapsed;
  47.  
  48.   Cpu_Time(&tstats.utime, &tstats.stime);
  49.   tstats.cur_time = tstats.utime + tstats.stime;
  50.   elapsed = tstats.cur_time - tstats.last_time;
  51.  
  52.   /* printf("last = %g, cur = %g\n", tstats.last_time, tstats.cur_time); */
  53.   tstats.last_time = tstats.cur_time;
  54.  
  55.   return elapsed;
  56. }
  57.  
  58. /**********************************************************************/
  59. /* Initialize time record                                             */
  60. /**********************************************************************/
  61. void Init_Time()
  62. {
  63.   tstats.tot_time = 0.0; 
  64.   tstats.last_time = 0.0;
  65.   tstats.cur_time = 0.0;
  66.   tstats.avg_ray = 0.0;
  67.   tstats.avg_shaft = 0.0;
  68.   tstats.avg_ff = 0.0;
  69.   tstats.avg_iter = 0.0;
  70.   tstats.prep_oHBV = 0.0;
  71.   tstats.prep_pHBV = 0.0;
  72.   tstats.prep_Model = 0.0;
  73. }
  74.  
  75. /**********************************************************************/
  76. /* Print times */
  77. /**********************************************************************/
  78. void Print_Times(fptr, use_shaft)
  79.      FILE *fptr;
  80.      int use_shaft;
  81. {
  82.   FILE *fp;
  83.  
  84.   if (Option.device == PRINT)
  85.     fp = stdout;
  86.   else 
  87.     fp = fptr;
  88.  
  89.   fprintf(fp,"\n\tCPU times (sec):\n");
  90.   fprintf(fp,  "\t----------------\n");
  91.   fprintf(fp,"\tProg. Refine: %g; Prep: (pHBV): %g; (oHBV): %g; (Mod): %g\n",
  92.       tstats.tot_time, tstats.prep_pHBV, tstats.prep_oHBV, 
  93.       tstats.prep_Model);
  94.   fprintf(fp,"\tAverage: ");
  95.   fprintf(fp,"iter(%g); ", tstats.avg_iter);
  96.   fprintf(fp,"ff(%g); ", tstats.avg_ff);
  97.   if (use_shaft) fprintf(fp,"shaft(%g); ",
  98.                      tstats.avg_shaft);
  99.   fprintf(fp,"ray(%g)\n\n", tstats.avg_ray);
  100.  
  101.   if (Option.tablelog) {
  102.     fp = rlogfile;
  103.     fprintf(fp,"%g \\\\\n%g \\\\\n%g \\\\\n%g \\\\\n",
  104.         tstats.tot_time, tstats.prep_Model,
  105.         tstats.prep_pHBV, tstats.prep_oHBV);
  106.     fprintf(fp,"%g \\\\\n", tstats.avg_iter);
  107.     fprintf(fp,"%g \\\\\n", tstats.avg_ff);
  108.     if (use_shaft) fprintf(fp,"%g \\\\\n",
  109.                tstats.avg_shaft);
  110.     fprintf(fp,"%g \\\\\n", tstats.avg_ray);
  111.   }
  112. }
  113.