Back to index

texmacs  1.0.7.15
timer.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : timer.cpp
00004 * DESCRIPTION: timers
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "timer.hpp"
00013 #ifdef OS_WIN32
00014 #include <sys/misc.h>
00015 #endif
00016 #include "iterator.hpp"
00017 #include "merge_sort.hpp"
00018 
00019 static hashmap<string,int> timing_level (0);
00020 static hashmap<string,int> timing_nr    (0);
00021 static hashmap<string,int> timing_cumul (0);
00022 static hashmap<string,int> timing_last  (0);
00023 
00024 /******************************************************************************
00025 * Getting the time
00026 ******************************************************************************/
00027 
00028 time_t
00029 raw_time () {
00030 #ifdef HAVE_GETTIMEOFDAY
00031   struct timeval tp;
00032   gettimeofday (&tp, NULL);
00033   return (time_t) ((tp.tv_sec * 1000) + (tp.tv_usec / 1000));
00034 #else
00035   timeb tb;
00036   ftime (&tb);
00037   return (time_t) ((tb.time * 1000) + tb.millitm);
00038 #endif
00039 }
00040 
00041 static time_t start_time= raw_time ();
00042 
00043 time_t
00044 texmacs_time () {
00045 #ifdef HAVE_GETTIMEOFDAY
00046   struct timeval tp;
00047   gettimeofday (&tp, NULL);
00048   return ((time_t) ((tp.tv_sec * 1000) + (tp.tv_usec / 1000))) - start_time;
00049 #else
00050   timeb tb;
00051   ftime (&tb);
00052   return ((time_t) ((tb.time * 1000) + tb.millitm)) - start_time;
00053 #endif
00054 }
00055 
00056 /******************************************************************************
00057 * Routines for benchmarking
00058 ******************************************************************************/
00059 
00060 void
00061 bench_start (string task) {
00062   // start timer for a given type of task
00063   if (timing_level [task] == 0)
00064     timing_last (task)= (int) texmacs_time ();
00065   timing_level (task) ++;
00066 }
00067 
00068 void
00069 bench_cumul (string task) {
00070   // end timer for a given type of task, but don't reset timer
00071   timing_level (task) --;
00072   if (timing_level [task] == 0) {
00073     int ms= ((int) texmacs_time ()) - timing_last (task);
00074     timing_nr    (task) ++;
00075     timing_cumul (task) += ms;
00076     timing_last -> reset (task);
00077   }
00078 }
00079 
00080 void
00081 bench_end (string task) {
00082   // end timer for a given type of task, print result and reset timer
00083   bench_cumul (task);
00084   bench_print (task);
00085   bench_reset (task);
00086 }
00087 
00088 void
00089 bench_reset (string task) {
00090   // reset timer for a given type of task
00091   timing_level->reset (task);
00092   timing_nr   ->reset (task);
00093   timing_cumul->reset (task);
00094   timing_last ->reset (task);
00095 }
00096 
00097 void
00098 bench_print (string task) {
00099   // print timing for a given type of task
00100   if (DEBUG_BENCH) {
00101     int nr= timing_nr [task];
00102     cout << "Timing ] Task '" << task
00103         << "' took " << timing_cumul [task] << " ms";
00104     if (nr > 1) cout << " (" << nr << " invocations)";
00105     cout << "\n";
00106   }
00107 }
00108 
00109 static array<string>
00110 collect (hashmap<string,int> h) {
00111   array<string> a;
00112   iterator<string> it= iterate (h);
00113   while (it->busy ())
00114     a << it->next ();
00115   merge_sort (a);
00116   return a;
00117 }
00118 
00119 void
00120 bench_print () {
00121   // print timings for all types of tasks
00122   array<string> a= collect (timing_cumul);
00123   int i, n= N(a);
00124   for (i=0; i<n; i++)
00125     bench_print (a[i]);
00126 }