eg_timer.h

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /* ========================================================================= */
00021 /** @defgroup EGtimer EGtimer 
00022  *
00023  * Here we implement types and functions for timer functions
00024  *
00025  * @version 1.0.1 
00026  * @par History:
00027  * - 2006-01-25
00028  *            - Fix compilation errors on sun, change includes accordingly and
00029  *            code
00030  * - 2005-05-31
00031  *            - Eliminate the definition of #EGwallClockTimer_t and replace it by
00032  *            a macro definition that replace it by #EGtimer_t.
00033  * - 2004-01-20
00034  *            - Add a 'wall clock' timer (Renan-Marcos) type and functions
00035  * - 2003-05-08
00036  *            - First Implementation
00037  * @note Up to now, this code will only work on linux machines, and maybe on
00038  * unix/posix systems.
00039  * */
00040 /** @file 
00041  * @ingroup EGtimer */
00042 /** @addtogroup EGtimer */
00043 /** @{ */
00044 /* ========================================================================= */
00045 
00046 #ifndef __EG_TIMER_H__
00047 #define __EG_TIMER_H__
00048 
00049 #include <stdlib.h>
00050 #include <stdio.h>
00051 #include "eg_config.h"
00052 #if OS == LINUX
00053 #include <sys/resource.h>
00054 
00055 /* ========================================================================= */
00056 /** @brief Get system time.
00057  *
00058  * This function is for internal purposes only and should not be called from the
00059  * user space, it ask the (user) time from the system. 
00060  * @return the time (in seconds) stored as a double. */
00061 #define __EGzeit() ({\
00062   struct rusage __EGzeit_ru;\
00063   int __EGzeit_st = getrusage(RUSAGE_SELF,&__EGzeit_ru);\
00064   EXIT(__EGzeit_st,"getrusage failed with code error %d (%s)", errno, \
00065   strerror(errno));\
00066   (((double)__EGzeit_ru.ru_utime.tv_sec) + \
00067   ((double)__EGzeit_ru.ru_utime.tv_usec)/1000000);})
00068 
00069 
00070 #elif OS == SUN
00071 # include <sys/param.h>
00072 # include <sys/times.h>
00073 #ifdef CLK_TCK
00074 #define MACHINE_FREQ CLK_TCK
00075 #else
00076 #define MACHINE_FREQ HZ
00077 #endif
00078 /* ========================================================================= */
00079 /** @brief Get system time.
00080  *
00081  * This function is for internal purposes only and should not be called from the
00082  * user space, it ask the (user) time from the system. 
00083  * @return the time (in seconds) stored as a double. */
00084 #define __EGzeit() ({\
00085   struct tms __EGzeit_now;\
00086   times(&__EGzeit_now);\
00087   ((double) __EGzeit_now.tms_utime)/((double) MACHINE_FREQ);})
00088 #else
00089 #error Unknown OS
00090 #endif
00091 #include <sys/time.h>
00092 #include <unistd.h>
00093 #include <time.h>
00094 #include <errno.h>
00095 #include "eg_macros.h"
00096 
00097 /* ========================================================================= */
00098 /** @brief this structure holds a timer structure */
00099 typedef struct
00100 {
00101   double time;  /**< hold the accumulated time */
00102   double stime; /**< hols the last time when we start counting, this is only 
00103                      for internal purposes, the user should only use the 
00104                      field 'time' */
00105 }
00106 EGtimer_t;
00107 
00108 /* ========================================================================= */
00109 /** @brief This is done for backward compability, we used to define
00110  * EGwallClockTimer_t just as the normal timer, so in reality we don't need
00111  * another type, but keep the name so that older code depending on this still
00112  * compiles. */
00113 #define EGwallClockTimer_t EGtimer_t
00114 
00115 /* ========================================================================= */
00116 /** @brief Set a new starting time for the timer.
00117  * @param timer pointer to a EGtimer_t structure.
00118  * @return starting time (in seconds), and the type is a double. */
00119 #define EGtimerStart(timer) ({(timer)->stime = __EGzeit();})
00120 
00121 /* ========================================================================= */
00122 /** @brief Stop a 'running' timer and accumulate the run time.
00123  * @return the time elapsed since the last 'start' call (in seconds). */
00124 #define EGtimerStop(timer) ({(timer)->time += __EGzeit() - (timer)->stime;})
00125 
00126 /* ========================================================================= */
00127 /** @brief this function reset the accumulated time to zero */
00128 #define EGtimerReset(timer) ({(timer)->time = 0;})
00129 
00130 /* ========================================================================= */
00131 /** @brief Set the starting time the current (wall) time.
00132  * @return the current wall time. */
00133 #define EGwallClockTimerStart(timer) ({(timer)->stime = time(0);})
00134 
00135 /* ========================================================================= */
00136 /** @brief Stop a 'running' timer and accumulate the (wall) runing time.
00137  * @return the wall time elapsed since the last initialization. */
00138 #define EGwallClockTimerStop(timer) ({\
00139   (timer)->time += difftime(time(0),(timer)->stime);})
00140 
00141 /* ========================================================================= */
00142 /** @brief Reset the accumulated time to zero */
00143 #define EGwallClockTimerReset(timer) EGtimerReset(timer)
00144 
00145 /* ========================================================================= */
00146 /** @}
00147  * end of eg_timer.h */
00148 #endif

Generated on Wed Nov 21 09:38:13 2007 for MTgomory by  doxygen 1.4.6