BCLS: Bound Constrained Least Squares

Version 0.1

bctimer.c

Go to the documentation of this file.
00001 /* bctimer.c
00002    $Revision: 237 $ $Date: 2006-04-19 18:42:26 -0700 (Wed, 19 Apr 2006) $
00003 
00004    ----------------------------------------------------------------------
00005    This file is part of BCLS (Bound-Constrained Least Squares).
00006 
00007    Copyright (C) 2006 Michael P. Friedlander, Department of Computer
00008    Science, University of British Columbia, Canada. All rights
00009    reserved. E-mail: <mpf@cs.ubc.ca>.
00010    
00011    BCLS is free software; you can redistribute it and/or modify it
00012    under the terms of the GNU Lesser General Public License as
00013    published by the Free Software Foundation; either version 2.1 of the
00014    License, or (at your option) any later version.
00015    
00016    BCLS is distributed in the hope that it will be useful, but WITHOUT
00017    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00018    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
00019    Public License for more details.
00020    
00021    You should have received a copy of the GNU Lesser General Public
00022    License along with BCLS; if not, write to the Free Software
00023    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00024    USA
00025    ----------------------------------------------------------------------
00026 */
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035 #include <assert.h>
00036 #include "bctimer.h"
00037 
00047 double
00048 bcls_timer( BCLS_timer *swatch, int task )
00049 {
00050     assert( task == BCLS_TIMER_START ||
00051             task == BCLS_TIMER_STOP  ||
00052             task == BCLS_TIMER_INIT  ||
00053             task == BCLS_TIMER_PRINT );
00054 
00055     double last; // Wall-clock time that the stopwatch was last called.
00056 
00057 #ifdef HAVE_GETRUSAGE
00058 
00059     struct rusage ru;
00060     (void) getrusage(RUSAGE_SELF, &ru);
00061 
00062     double user_time = 
00063                  ru.ru_utime.tv_sec   // user time (seconds)
00064         + 1e-6 * ru.ru_utime.tv_usec; // user time (microseconds)
00065 
00066     double sys_time = 
00067                  ru.ru_stime.tv_sec   // system time (seconds)
00068         + 1e-6 * ru.ru_stime.tv_usec; // system time (microseconds)
00069 
00070     last = user_time + sys_time;
00071 
00072 #else
00073 
00074     last = (double)clock();
00075 
00076 #endif
00077 
00078     if ( task == BCLS_TIMER_START ) {
00079         swatch->start = last;
00080         swatch->nStarts++;
00081     }
00082     else if ( task == BCLS_TIMER_STOP ) {
00083 
00084 #ifdef HAVE_GETRUSAGE
00085 
00086         swatch->total += last - swatch->start;
00087 
00088 #else
00089 
00090         swatch->total += (last - swatch->start)
00091                              / ((double)CLOCKS_PER_SEC);
00092 
00093 #endif
00094 
00095     }
00096     else if ( task == BCLS_TIMER_INIT ) {
00097         swatch->total   = 0.0;
00098         swatch->nStarts = 0;
00099     }
00100     else if ( task == BCLS_TIMER_PRINT ) {
00101         return swatch->total;
00102     }
00103 
00104     return 0.0;
00105 }

Generated on Sun Mar 4 22:50:03 2007 by Doxygen 1.5.1