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 #include "eg_config.h"
00049 #include "eg_macros.h"
00050 
00051 /* ========================================================================= */
00052 /** @brief Get system time.
00053  *
00054  * This function is for internal purposes only and should not be called from the
00055  * user space, it ask the (user) time from the system. 
00056  * @return the time (in seconds) stored as a double. */
00057 #ifdef HAVE_GETRUSAGE
00058 #define __EGzeit() ({\
00059   struct rusage __EGzeit_ru;\
00060   int __EGzeit_st = getrusage(RUSAGE_SELF,&__EGzeit_ru);\
00061   EXIT(__EGzeit_st,"getrusage failed with code error %d (%s)", errno, \
00062   strerror(errno));\
00063   (((double)__EGzeit_ru.ru_utime.tv_sec) + \
00064   ((double)__EGzeit_ru.ru_utime.tv_usec)/1000000);})
00065 #else
00066 #ifdef HAVE_TIMES
00067 #ifdef CLK_TCK
00068 #define MACHINE_FREQ CLK_TCK
00069 #else
00070 #define MACHINE_FREQ HZ
00071 #endif
00072 #define __EGzeit() ({\
00073   struct tms __EGzeit_now;\
00074   times(&__EGzeit_now);\
00075   ((double) __EGzeit_now.tms_utime)/((double) MACHINE_FREQ);})
00076 #else
00077 #error Your system does not have (or the configure script could not find)\
00078  getrusage nor times functions, and thus we are unable to provide \
00079  timing functions. Without them this library will not compile in this system
00080 #endif
00081 #endif
00082 
00083 /* ========================================================================= */
00084 /** @brief this structure holds a __timer structure */
00085 typedef struct
00086 {
00087   double time;  /**< hold the accumulated time */
00088   double stime; /**< hols the last time when we start counting, this is only 
00089                      for internal purposes, the user should only use the 
00090                      field 'time' */
00091 }
00092 EGtimer_t;
00093 
00094 /* ========================================================================= */
00095 /** @brief This is done for backward compability, we used to define
00096  * EGwallClockTimer_t just as the normal __timer, so in reality we don't need
00097  * another type, but keep the name so that older code depending on this still
00098  * compiles. */
00099 #define EGwallClockTimer_t EGtimer_t
00100 
00101 /* ========================================================================= */
00102 /** @brief Set a new starting time for the __timer.
00103  * @param __timer pointer to a EGtimer_t structure.
00104  * @return starting time (in seconds), and the type is a double. */
00105 #define EGtimerStart(__timer) ({(__timer)->stime = __EGzeit();})
00106 
00107 /* ========================================================================= */
00108 /** @brief Stop a 'running' __timer and accumulate the run time.
00109  * @return the time elapsed since the last 'start' call (in seconds). */
00110 #define EGtimerStop(__timer) ({(__timer)->time += __EGzeit() - (__timer)->stime;})
00111 
00112 /* ========================================================================= */
00113 /** @brief this function reset the accumulated time to zero */
00114 #define EGtimerReset(__timer) ({(__timer)->time = 0;})
00115 
00116 /* ========================================================================= */
00117 /** @brief Set the starting time the current (wall) time.
00118  * @return the current wall time. */
00119 #define EGwallClockTimerStart(__timer) ({(__timer)->stime = time(0);})
00120 
00121 /* ========================================================================= */
00122 /** @brief Stop a 'running' __timer and accumulate the (wall) runing time.
00123  * @return the wall time elapsed since the last initialization. */
00124 #define EGwallClockTimerStop(__timer) ({\
00125   (__timer)->time += difftime(time(0),(__timer)->stime);})
00126 
00127 /* ========================================================================= */
00128 /** @brief Reset the accumulated time to zero */
00129 #define EGwallClockTimerReset(__timer) EGtimerReset(__timer)
00130 
00131 /* ========================================================================= */
00132 /** @}
00133  * end of eg_timer.h */
00134 #endif