timer-nt.c

Go to the documentation of this file.
00001 /*                      T I M E R - N T . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** @addtogroup timer */
00023 /*@{*/
00024 /** @file timer-nt.c
00025  * To provide timing information on Microsoft Windows NT.
00026  */
00027 
00028 #ifndef lint
00029 static const char RCStimer_nt[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/timer-nt.c,v 14.9 2006/09/16 02:04:26 lbutler Exp $ (BRL)";
00030 #endif
00031 
00032 #include "common.h"
00033 
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <time.h>
00037 
00038 #include "machine.h"
00039 #include "vmath.h"
00040 #include "raytrace.h"
00041 
00042 /* Standard System V stuff */
00043 static clock_t start;
00044 time_t time0;
00045 
00046 /*
00047  *                      P R E P _ T I M E R
00048  */
00049 void
00050 rt_prep_timer(void)
00051 {
00052         start = clock();
00053         time( &time0 );
00054 }
00055 
00056 /*
00057  *                      R T _ G E T _ T I M E R
00058  *
00059  *  Reports on the passage of time, since rt_prep_timer() was called.
00060  *  Explicit return is number of CPU seconds.
00061  *  String return is descriptive.
00062  *  If "elapsed" pointer is non-null, number of elapsed seconds are returned.
00063  *  Times returned will never be zero.
00064  */
00065 double
00066 rt_get_timer(struct bu_vls      *vp, double *elapsed)
00067 {
00068         long    now;
00069         double  user_cpu_secs;
00070         double  sys_cpu_secs;
00071         double  elapsed_secs;
00072         double  percent;
00073         clock_t finish;
00074 
00075         /* Real time.  1 second resolution. */
00076         (void)time(&now);
00077         elapsed_secs = difftime(now,time0);
00078 
00079         finish = clock();
00080         sys_cpu_secs = (double)(finish - start);
00081         sys_cpu_secs /= CLOCKS_PER_SEC;
00082 
00083         user_cpu_secs = sys_cpu_secs;
00084 
00085         if( user_cpu_secs < 0.00001 )  user_cpu_secs = 0.00001;
00086         if( elapsed_secs < 0.00001 )  elapsed_secs = user_cpu_secs;     /* It can't be any less! */
00087 
00088         if( elapsed )  *elapsed = elapsed_secs;
00089 
00090         if( vp )  {
00091                 percent = user_cpu_secs/elapsed_secs*100.0;
00092                 BU_CK_VLS(vp);
00093                 bu_vls_printf( vp,
00094                         "%g user + %g sys in %g elapsed secs (%g%%)",
00095                         user_cpu_secs, sys_cpu_secs, elapsed_secs, percent );
00096         }
00097         return( user_cpu_secs );
00098 }
00099 
00100 double
00101 rt_read_timer(char *str, int len)
00102 {
00103         struct bu_vls   vls;
00104         double          cpu;
00105         int             todo;
00106 
00107         if( !str )  return  rt_get_timer( (struct bu_vls *)0, (double *)0 );
00108 
00109         bu_vls_init( &vls );
00110         cpu = rt_get_timer( &vls, (double *)0 );
00111         todo = bu_vls_strlen( &vls );
00112         if( todo > len )  todo = len-1;
00113         strncpy( str, bu_vls_addr(&vls), todo );
00114         str[todo] = '\0';
00115         return cpu;
00116 }
00117 
00118 /*@}*/
00119 /*
00120  * Local Variables:
00121  * mode: C
00122  * tab-width: 8
00123  * c-basic-offset: 4
00124  * indent-tabs-mode: t
00125  * End:
00126  * ex: shiftwidth=4 tabstop=8
00127  */

Generated on Mon Sep 18 01:24:57 2006 for BRL-CAD by  doxygen 1.4.6