diff --git a/time_tracer.cc b/time_tracer.cc new file mode 100644 index 0000000000000000000000000000000000000000..5502e920acbde107c824193c3009006c22a26bb5 --- /dev/null +++ b/time_tracer.cc @@ -0,0 +1,28 @@ +/* Time_Tracer.cc + + Mark Woolrich, FMRIB Image Analysis Group + + Copyright (C) 1999-2000 University of Oxford */ + +/* CCOPYRIGHT */ + +#include "time_tracer.h" +#include <iostream> +#include <fstream> +#include <strstream> +#include <string> +#include <set> + +namespace Utilities { + + bool Time_Tracer::debug = false; + bool Time_Tracer::timingon = false; + unsigned int Time_Tracer::pad = 0; + set<TimingFunction*, TimingFunction::comparer> Time_Tracer::timingFunctions; +} + + + + + + diff --git a/time_tracer.h b/time_tracer.h new file mode 100644 index 0000000000000000000000000000000000000000..a0d77afac1a6d88e85453a6ec0138b8b64abab60 --- /dev/null +++ b/time_tracer.h @@ -0,0 +1,144 @@ +/* Time_Tracer.h + + Mark Woolrich, FMRIB Image Analysis Group + + Copyright (C) 1999-2000 University of Oxford */ + +/* CCOPYRIGHT */ + +#if !defined(Time_Tracer_h) +#define Time_Tracer_h + +#include <iostream> +#include <fstream> +#include <string> +#include <time.h> +#include <set> + +namespace Utilities{ + + class TimingFunction + { + public: + TimingFunction(const char * pstr): + str(pstr), + time_taken(0.0), + times_called(0) + {} + + class comparer + { + public: + bool operator()(const TimingFunction* t1, const TimingFunction* t2) const + { + return strcmp(t1->str, t2->str) < 0; + } + }; + + void start() {start_time = clock();} + void end() {time_taken += clock()-start_time; times_called++;} + + friend comparer; + friend ostream& operator<<(ostream& ostr, const TimingFunction* t); + + protected: + const char* str; + clock_t time_taken; + int times_called; + clock_t start_time; + + private: + TimingFunction(); + const TimingFunction& operator=(TimingFunction&); + TimingFunction(TimingFunction&); + }; + + inline ostream& operator<<(ostream& ostr, const TimingFunction* t) + { + ostr << "<tr><td>" << t->str; + ostr.setf(0, ios::floatfield); + ostr << "<td align=center>" << t->time_taken/CLOCKS_PER_SEC; + ostr.setf(ios::scientific, ios::floatfield); + ostr << "<td align=center>" << t->times_called << "<td align=center>" << (t->time_taken/t->times_called)/CLOCKS_PER_SEC; + ostr << "</tr>"; + return ostr; + } + + // Non Newmat Tracer: + class Time_Tracer + { + public: + Time_Tracer(char* str) + { + if(debug) + { + tmp = ""; + pad++; + for(unsigned int i = 0; i < pad; i++) + tmp = tmp + " "; + + cout << tmp << str << endl; + } + if(timingon) + { + // see if already in list: + timingFunction = new TimingFunction(str); + set<TimingFunction*, TimingFunction::comparer>::iterator it = timingFunctions.find(timingFunction); + if(it== timingFunctions.end()) + { + timingFunctions.insert(timingFunction); + } + else + { + timingFunction = *it; + } + + timingFunction->start(); + } + } + + virtual ~Time_Tracer() + { + if(debug && pad > 0) + { + cout << tmp << "finished" << endl; + pad--; + } + if(timingon) + { + timingFunction->end(); + } + } + + static void dump_times(const string& dir) + { + ofstream out; + out.open((dir + "/timings.html").c_str(), ios::out); + out << "<HTML><TITLE>Tracer Timings</TITLE><BODY><table border=3 cellspacing=5>" << endl; + out << "<tr><td>Function<td align=center>Total Time(secs)<td align=center>Num of calls<td align=center>Time per call(secs)</tr>" << endl; + copy(timingFunctions.begin(), timingFunctions.end(), ostream_iterator<TimingFunction*>(out, "\n")); + out << "</table></BODY></HTML>" << endl; + out.close(); + } + + static void setdebugon() {debug = true;} + static void settimingon() {timingon = true;} + + protected: + static bool debug; + static bool timingon; + static unsigned int pad; + static set<TimingFunction*, TimingFunction::comparer> timingFunctions; + + string tmp; + TimingFunction* timingFunction; + + private: + Time_Tracer(); + const Time_Tracer& operator=(Time_Tracer&); + Time_Tracer(Time_Tracer&); + }; + +} +#endif + diff --git a/tracer_plus.h b/tracer_plus.h new file mode 100644 index 0000000000000000000000000000000000000000..594bc58f44226796baae3559613413e36b3058e6 --- /dev/null +++ b/tracer_plus.h @@ -0,0 +1,48 @@ +/* Tracer_Plus.h + + Mark Woolrich, FMRIB Image Analysis Group + + Copyright (C) 1999-2000 University of Oxford */ + +/* CCOPYRIGHT */ + +#if !defined(Tracer_Plus_h) +#define Tracer_Plus_h + +#include <iostream> +#include <fstream> +#include <string> +#include <time.h> +#include <set> +#include "newmatap.h" +#include "newmatio.h" +#include "time_tracer.h" + +using namespace NEWMAT; + +namespace Utilities { + + // Newmat version: + class Tracer_Plus : public Tracer, public Time_Tracer + { + public: + Tracer_Plus(char* str) : + Tracer(str), + Time_Tracer(str) + { + } + + virtual ~Tracer_Plus() + { + } + + private: + Tracer_Plus(); + const Tracer_Plus& operator=(Tracer_Plus&); + Tracer_Plus(Tracer_Plus&); + }; + +} + +#endif +