Commit 513fa6fa authored by Davis King's avatar Davis King

Changed timing routines so you can name each timing block.

parent dd2a6468
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#define DLIB_TImING_H__ #define DLIB_TImING_H__
#include "misc_api.h" #include "misc_api.h"
#include <cstring>
#include "string.h"
#include <iostream> #include <iostream>
...@@ -18,12 +20,12 @@ ...@@ -18,12 +20,12 @@
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
// timing block #1 // timing block #1
start(1); start(1,"block #1");
dlib::sleep(500); dlib::sleep(500);
stop(1); stop(1);
// timing block #2 // timing block #2
start(2); start(2,"block #2");
dlib::sleep(1000); dlib::sleep(1000);
stop(2); stop(2);
} }
...@@ -33,8 +35,8 @@ ...@@ -33,8 +35,8 @@
This program would output: This program would output:
Timing report: Timing report:
1: 5.0 seconds block #1: 5.0 seconds
2: 10.0 seconds block #2: 10.0 seconds
So we spent 5 seconds in block #1 and 10 seconds in block #2 So we spent 5 seconds in block #1 and 10 seconds in block #2
!*/ !*/
...@@ -44,6 +46,7 @@ namespace dlib ...@@ -44,6 +46,7 @@ namespace dlib
namespace timing namespace timing
{ {
const int TIME_SLOTS = 500; const int TIME_SLOTS = 500;
const int NAME_LENGTH = 40;
inline uint64* time_buf() inline uint64* time_buf()
{ {
...@@ -51,6 +54,20 @@ namespace dlib ...@@ -51,6 +54,20 @@ namespace dlib
return buf; return buf;
} }
inline char* name_buf(int i, const char* name)
{
static char buf[TIME_SLOTS][NAME_LENGTH] = {{0}};
// if this name buffer is empty then copy name into it
if (buf[i][0] == '\0')
{
using namespace std;
strncpy(buf[i], name, NAME_LENGTH-1);
buf[i][NAME_LENGTH-1] = '\0';
}
// return the name buffer
return buf[i];
}
inline timestamper& ts() inline timestamper& ts()
{ {
static timestamper ts_; static timestamper ts_;
...@@ -62,6 +79,12 @@ namespace dlib ...@@ -62,6 +79,12 @@ namespace dlib
time_buf()[i] -= ts().get_timestamp(); time_buf()[i] -= ts().get_timestamp();
} }
inline void start(int i, const char* name)
{
time_buf()[i] -= ts().get_timestamp();
name_buf(i,name);
}
inline void stop(int i) inline void stop(int i)
{ {
time_buf()[i] += ts().get_timestamp(); time_buf()[i] += ts().get_timestamp();
...@@ -71,22 +94,71 @@ namespace dlib ...@@ -71,22 +94,71 @@ namespace dlib
{ {
using namespace std; using namespace std;
cout << "Timing report: " << endl; cout << "Timing report: " << endl;
// figure out how long the longest name is going to be.
unsigned long max_name_length = 0;
for (int i = 0; i < TIME_SLOTS; ++i)
{
string name;
// Check if the name buffer is empty. Use the name it contains if it isn't.
if (name_buf(i,"")[0] != '\0')
name = name_buf(i,"");
else
name = cast_to_string(i);
max_name_length = std::max(max_name_length, name.size());
}
for (int i = 0; i < TIME_SLOTS; ++i) for (int i = 0; i < TIME_SLOTS; ++i)
{ {
if (time_buf()[i] != 0) if (time_buf()[i] != 0)
{ {
double time = time_buf()[i]/1000.0; double time = time_buf()[i]/1000.0;
string name;
// Check if the name buffer is empty. Use the name it contains if it isn't.
if (name_buf(i,"")[0] != '\0')
name = name_buf(i,"");
else
name = cast_to_string(i);
// make sure the name is always the same length. Do so by padding with spaces
if (name.size() < max_name_length)
name += string(max_name_length-name.size(),' ');
if (time < 1000) if (time < 1000)
cout << " " << i << ": " << time << " milliseconds" << endl; cout << " " << name << ": " << time << " milliseconds" << endl;
else if (time < 1000*1000) else if (time < 1000*1000)
cout << " " << i << ": " << time/1000.0 << " seconds" << endl; cout << " " << name << ": " << time/1000.0 << " seconds" << endl;
else if (time < 1000*1000*60) else if (time < 1000*1000*60)
cout << " " << i << ": " << time/1000.0/60.0 << " minutes" << endl; cout << " " << name << ": " << time/1000.0/60.0 << " minutes" << endl;
else else
cout << " " << i << ": " << time/1000.0/60.0/60.0 << " hours" << endl; cout << " " << name << ": " << time/1000.0/60.0/60.0 << " hours" << endl;
} }
} }
} }
inline void clear()
{
for (int i = 0; i < TIME_SLOTS; ++i)
{
// clear timing buffer
time_buf()[i] = 0;
// clear name buffer
name_buf(i,"")[0] = '\0';
}
}
struct block
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an RAII tool for calling start() and stop()
!*/
block(int i):idx(i) {start(idx);}
block(int i, const char* str):idx(i) {start(idx,str);}
~block() { stop(idx); }
const int idx;
};
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment