-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: 沈浪熊猫儿 <[email protected]>
- Loading branch information
Showing
4 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
/****************************************************************************** | ||
* MODULE : timer.cpp | ||
* DESCRIPTION: timers | ||
* COPYRIGHT : (C) 1999 Joris van der Hoeven | ||
******************************************************************************* | ||
* This software falls under the GNU general public license version 3 or later. | ||
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE | ||
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
******************************************************************************/ | ||
|
||
#include "tm_timer.hpp" | ||
#include "basic.hpp" | ||
#include "iterator.hpp" | ||
#include "merge_sort.hpp" | ||
#include "tm_ostream.hpp" | ||
|
||
static hashmap<string, int> timing_level (0); | ||
static hashmap<string, int> timing_nr (0); | ||
static hashmap<string, int> timing_cumul (0); | ||
static hashmap<string, int> timing_last (0); | ||
|
||
/****************************************************************************** | ||
* Getting the time | ||
******************************************************************************/ | ||
|
||
time_t | ||
raw_time () { | ||
#ifdef HAVE_GETTIMEOFDAY | ||
struct timeval tp; | ||
gettimeofday (&tp, NULL); | ||
return (time_t) ((tp.tv_sec * 1000) + (tp.tv_usec / 1000)); | ||
#else | ||
timeb tb; | ||
ftime (&tb); | ||
return (time_t) ((tb.time * 1000) + tb.millitm); | ||
#endif | ||
} | ||
|
||
static time_t start_time= raw_time (); | ||
|
||
time_t | ||
texmacs_time () { | ||
#ifdef HAVE_GETTIMEOFDAY | ||
struct timeval tp; | ||
gettimeofday (&tp, NULL); | ||
return ((time_t) ((tp.tv_sec * 1000) + (tp.tv_usec / 1000))) - start_time; | ||
#else | ||
timeb tb; | ||
ftime (&tb); | ||
return ((time_t) ((tb.time * 1000) + tb.millitm)) - start_time; | ||
#endif | ||
} | ||
|
||
/****************************************************************************** | ||
* Routines for benchmarking | ||
******************************************************************************/ | ||
|
||
void | ||
bench_start (string task) { | ||
// start timer for a given type of task | ||
if (timing_level[task] == 0) timing_last (task)= (int) texmacs_time (); | ||
timing_level (task)++; | ||
} | ||
|
||
void | ||
bench_cumul (string task) { | ||
// end timer for a given type of task, but don't reset timer | ||
timing_level (task)--; | ||
if (timing_level[task] == 0) { | ||
int ms= ((int) texmacs_time ()) - timing_last (task); | ||
timing_nr (task)++; | ||
timing_cumul (task)+= ms; | ||
timing_last->reset (task); | ||
} | ||
} | ||
|
||
void | ||
bench_end (tm_ostream ostream, string task) { | ||
// end timer for a given type of task, print result and reset timer | ||
bench_cumul (task); | ||
bench_print (ostream, task); | ||
bench_reset (task); | ||
} | ||
|
||
void | ||
bench_reset (string task) { | ||
// reset timer for a given type of task | ||
timing_level->reset (task); | ||
timing_nr->reset (task); | ||
timing_cumul->reset (task); | ||
timing_last->reset (task); | ||
} | ||
|
||
void | ||
bench_print (tm_ostream ostream, string task) { | ||
// print timing for a given type of task | ||
if (DEBUG_BENCH) { | ||
int nr= timing_nr[task]; | ||
ostream << "Task '" << task << "' took " << timing_cumul[task] << " ms"; | ||
if (nr > 1) ostream << " (" << nr << " invocations)"; | ||
ostream << LF; | ||
} | ||
} | ||
|
||
static array<string> | ||
collect (hashmap<string, int> h) { | ||
array<string> a; | ||
iterator<string> it= iterate (h); | ||
while (it->busy ()) | ||
a << it->next (); | ||
merge_sort (a); | ||
return a; | ||
} | ||
|
||
void | ||
bench_print (tm_ostream ostream) { | ||
// print timings for all types of tasks | ||
array<string> a= collect (timing_cumul); | ||
int i, n= N (a); | ||
for (i= 0; i < n; i++) | ||
bench_print (ostream, a[i]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
/****************************************************************************** | ||
* MODULE : tm_timer.hpp | ||
* DESCRIPTION: timers | ||
* COPYRIGHT : (C) 1999 Joris van der Hoeven | ||
******************************************************************************* | ||
* This software falls under the GNU general public license version 3 or later. | ||
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE | ||
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
******************************************************************************/ | ||
|
||
#ifndef TIMER_H | ||
#define TIMER_H | ||
|
||
#include "string.hpp" | ||
#include "tm_ostream.hpp" | ||
|
||
#ifndef __FreeBSD__ | ||
#ifndef HAVE_TIME_T | ||
#define HAVE_TIME_T | ||
typedef long time_t; | ||
#endif | ||
#else | ||
#include <time.h> | ||
#endif | ||
|
||
#ifdef OS_SUN | ||
#include <sys/types.h> | ||
#endif | ||
|
||
#ifdef HAVE_GETTIMEOFDAY | ||
#include <sys/time.h> | ||
#else | ||
#include <sys/timeb.h> | ||
#ifdef OS_SUN | ||
extern "C" { | ||
extern int ftime __P ((struct timeb * __timebuf)); | ||
}; | ||
#endif | ||
#endif | ||
|
||
time_t raw_time (); | ||
time_t texmacs_time (); | ||
|
||
void bench_start (string task); | ||
void bench_cumul (string task); | ||
void bench_end (tm_ostream ostream, string task); | ||
void bench_reset (string task); | ||
void bench_print (tm_ostream ostream, string task); | ||
void bench_print (tm_ostream ostream); | ||
|
||
#endif // defined TIMER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters