Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
513fa6fa
Commit
513fa6fa
authored
Jan 22, 2012
by
Davis King
Browse files
Changed timing routines so you can name each timing block.
parent
dd2a6468
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
8 deletions
+80
-8
dlib/timing.h
dlib/timing.h
+80
-8
No files found.
dlib/timing.h
View file @
513fa6fa
...
...
@@ -4,6 +4,8 @@
#define DLIB_TImING_H__
#include "misc_api.h"
#include <cstring>
#include "string.h"
#include <iostream>
...
...
@@ -18,12 +20,12 @@
for (int i = 0; i < 10; ++i)
{
// timing block #1
start(1);
start(1
,"block #1"
);
dlib::sleep(500);
stop(1);
// timing block #2
start(2);
start(2
,"block #2"
);
dlib::sleep(1000);
stop(2);
}
...
...
@@ -33,8 +35,8 @@
This program would output:
Timing report:
1: 5.0 seconds
2: 10.0 seconds
block #
1: 5.0 seconds
block #
2: 10.0 seconds
So we spent 5 seconds in block #1 and 10 seconds in block #2
!*/
...
...
@@ -44,6 +46,7 @@ namespace dlib
namespace
timing
{
const
int
TIME_SLOTS
=
500
;
const
int
NAME_LENGTH
=
40
;
inline
uint64
*
time_buf
()
{
...
...
@@ -51,6 +54,20 @@ namespace dlib
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
()
{
static
timestamper
ts_
;
...
...
@@ -62,6 +79,12 @@ namespace dlib
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
)
{
time_buf
()[
i
]
+=
ts
().
get_timestamp
();
...
...
@@ -71,22 +94,71 @@ namespace dlib
{
using
namespace
std
;
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
)
{
if
(
time_buf
()[
i
]
!=
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
)
cout
<<
" "
<<
i
<<
": "
<<
time
<<
" milliseconds"
<<
endl
;
cout
<<
" "
<<
name
<<
": "
<<
time
<<
" milliseconds"
<<
endl
;
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
)
cout
<<
" "
<<
i
<<
": "
<<
time
/
1000.0
/
60.0
<<
" minutes"
<<
endl
;
cout
<<
" "
<<
name
<<
": "
<<
time
/
1000.0
/
60.0
<<
" minutes"
<<
endl
;
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
;
};
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment