An illustrative code example in C++ using the performance timer API is shown below (and is also available in the examples directory from a top-level install). This example illustrates basic timer usage (including the use of nested timers).
#include<sys/time.h>
#include<time.h>
#include<unistd.h>
#define FUNC_BEGIN_TIMER gt.BeginTimer(__func__);
#define FUNC_END_TIMER gt.EndTimer (__func__);
GRVY::GRVY_Timer_Class gt;
const double Foo_Sleep = 0.3 * 1.e6;
const double Bar_Sleep = 0.05 * 1.e6;
const double Boo_Sleep = 0.1167 * 1.e6;
const double Max_Iters = 10;
void foo();
void bar();
void boo();
int main()
{
int i,itest;
int num_repeat = 2;
gt.Init("GRVY Example Timing");
for(itest=0;itest<num_repeat;itest++)
{
if(itest > 0)
gt.Reset();
gt.BeginTimer("Main Program");
for(i=0;i<Max_Iters;i++)
{
foo();
}
gt.EndTimer("Main Program");
gt.Finalize();
gt.Summarize();
}
grvy_printf(GRVY_INFO,
"Querying global timer elapsed time:\n");
grvy_printf(GRVY_INFO,
"\tElapsed global time = %10.5e secs\n",gt.ElapsedGlobal());
grvy_printf(GRVY_INFO,
"\nQuerying individual timers (exclusive values):\n");
grvy_printf(GRVY_INFO,
"\tfoo: %10.5e secs\n",gt.ElapsedSeconds(
"foo"));
grvy_printf(GRVY_INFO,
"\tbar: %10.5e secs\n",gt.ElapsedSeconds(
"bar"));
grvy_printf(GRVY_INFO,
"\tboo: %10.5e secs\n",gt.ElapsedSeconds(
"boo"));
grvy_printf(GRVY_INFO,
"\nQuerying individual stats for timer \"bar\" (exclusive values)\n");
grvy_printf(GRVY_INFO,
"\tbar ( count): %i\n",gt.StatsCount (
"bar"));
grvy_printf(GRVY_INFO,
"\tbar ( mean): %e\n",gt.StatsMean (
"bar"));
grvy_printf(GRVY_INFO,
"\tbar (variance): %e\n",gt.StatsVariance(
"bar"));
grvy_printf(GRVY_INFO,
"\tbar ( min): %e\n",gt.StatsMin (
"bar"));
grvy_printf(GRVY_INFO,
"\tbar ( max): %e\n",gt.StatsMax (
"bar"));
grvy_printf(GRVY_INFO,
"\nQuerying individual timers (inclusive values):\n");
grvy_printf(GRVY_INFO,
"\tfoo: %10.5e secs\n",gt.ElapsedSeconds_inc(
"foo"));
grvy_printf(GRVY_INFO,
"\tbar: %10.5e secs\n",gt.ElapsedSeconds_inc(
"bar"));
grvy_printf(GRVY_INFO,
"\tboo: %10.5e secs\n",gt.ElapsedSeconds_inc(
"boo"));
grvy_printf(GRVY_INFO,
"\nQuerying individual stats for timer \"foo\" (inclusive values)\n");
grvy_printf(GRVY_INFO,
"\tbar ( mean): %e\n",gt.StatsMean_inc (
"foo"));
grvy_printf(GRVY_INFO,
"\tbar (variance): %e\n",gt.StatsVariance_inc(
"foo"));
grvy_printf(GRVY_INFO,
"\tbar ( min): %e\n",gt.StatsMin_inc (
"foo"));
grvy_printf(GRVY_INFO,
"\tbar ( max): %e\n",gt.StatsMax_inc (
"foo"));
return 0;
}
void foo()
{
FUNC_BEGIN_TIMER
usleep(Foo_Sleep);
bar();
FUNC_END_TIMER
return;
}
void bar()
{
FUNC_BEGIN_TIMER
usleep(Bar_Sleep);
boo();
FUNC_END_TIMER
return;
}
void boo()
{
FUNC_BEGIN_TIMER
usleep(Boo_Sleep);
FUNC_END_TIMER
return;
}