performance/timeroverhead.c
///////////////////////////////////////////////////////////////////////////////
// Filename: timeroverhead.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: measures the overhead of using the timer classes
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/14 00:58:48 muellerg: created
// 96/03/11 00:41:11 muellerg: removed some parts from compilation, as their
//                             result says more or less nothing
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */
// System headers /////////////////////////////////////////// System headers //
#include <stdlib.h>
// Local headers ///////////////////////////////////////////// Local headers //
#include "../common.h"
// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */
// File scope objects /////////////////////////////////// File scope objects //
const int NUMBER_REPEAT = 1000000; /* how often are clock measurements done? */
// External variables, functions, and classes ///////////// External objects //
    /* NONE */
// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */
// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */
// Functions and class implementation /// Functions and class implementation //
    /* NONE */
// Main /////////////////////////////////////////////////////////////// Main //
int
main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    
    int i;
    cout << "This program measures the overhead in using the timer classes "
         << "\"cputimer\" and \"clocktimer\"" << endl << endl;
#ifdef pedantic
    cout << "First all measurements are made once..." << endl << endl;
    cout << "Measuring the overhead of clocktimer..." << endl;
#endif
    clocktimer c1;
    // do dummy operations, as Linux needs about 470 usecs for the first
    // time... strange... probably some internal things have to be
    // initialized...
    c1.start();             // start clock timer
    c1.end();               // end clock timer
#ifdef pedantic
    c1.start();             // start clock timer
    c1.end();               // end clock timer
    cout << "Overhead: clock secs:" << c1.secs()
         << "  clock usecs:" << c1.usecs() << endl << endl;
    cout << "Measuring the overhead of cputimer..." << endl;
#endif
    cputimer c2;
#ifdef pedantic
    c2.start();             // start cpu timer
    c2.end();               // end cpu timer
    cout << "Overhead: cpu secs:" << c2.secs()
         << "  cpu usecs:" << c2.usecs() << endl << endl;
    cout << "Measuring the overhead of clocktimer and cputimer..." << endl;
    c1.start(); 
    c2.start();
    c2.end();   
    c1.end();
    cout << "Overhead: clock secs:" << c1.secs()
         << "  clock usecs:" << c1.usecs() << endl;
    cout << "Overhead: cpu secs:" << c2.secs()
         << "  cpu usecs:" << c2.usecs() << endl << endl;
    cout << "Second ";
#endif
    cout << "all measurements are made " << NUMBER_REPEAT 
         << " times..." << endl << endl;
    cout << "Measuring the overhead of the " << NUMBER_REPEAT 
         << "-loop..." << endl;
    c1.start(); 
    c2.start();
    // This measurement is not bullet-proof: the compiler might optimize
    // this loop much easier as the following ones. But it is better
    // than nothing...
    for(i=0; i < NUMBER_REPEAT; i++)
    {
        ;
    }
    
    c2.end();   
    c1.end();
    cout << "Overhead: clock secs:" << c1.secs() 
         << "  clock usecs:" << c1.usecs() << endl;
    cout << "Overhead: cpu secs:" << c2.secs()
         << "  cpu usecs:" << c2.usecs() << endl << endl;
    cout << "Measuring the overhead of clocktimer..." << endl;
    clocktimer  temp1;
    cputimer    temp2;
    c1.start(); 
    c2.start();
    for(i=0; i < NUMBER_REPEAT; i++)
    {
        temp1.start();
        temp1.end();
    }
    
    c2.end();   
    c1.end();
    cout << "Overhead: clock secs:" << c1.secs()
         << "  clock usecs:" << c1.usecs() << endl;
    cout << "Overhead: cpu secs:" << c2.secs()
         << "  cpu usecs:" << c2.usecs() << endl;
    cout << "Average times:"
         << "  clock secs:" << (float)(c1.secs()+(c1.usecs()/1000000.0)) / 
                                                        (float)NUMBER_REPEAT
         << "(=" << (c1.secs()*1000000.0+c1.usecs())/NUMBER_REPEAT << " usecs) 
         << "  cpu secs:" << (float)(c2.secs()+(c2.usecs()/1000000.0)) / 
                                                        (float)NUMBER_REPEAT 
         << "(=" << (c2.secs()*1000000.0+c2.usecs())/NUMBER_REPEAT << " usecs)"
         << endl << endl;
    cout << "Measuring the overhead of cputimer..." << endl;
    c1.start(); 
    c2.start();
    for(i=0; i < NUMBER_REPEAT; i++)
    {
        temp2.start();
        temp2.end();
    }
    
    c2.end();   
    c1.end();
    cout << "Overhead: clock secs:" << c1.secs()
         << "  clock usecs:" << c1.usecs() << endl;
    cout << "Overhead: cpu secs:" << c2.secs()
         << "  cpu usecs:" << c2.usecs() << endl;
    cout << "Average times:"
         << "  clock secs:" << (float)(c1.secs()+(c1.usecs()/1000000.0)) / 
                                                        (float)NUMBER_REPEAT
         << "(=" << (c1.secs()*1000000.0+c1.usecs())/NUMBER_REPEAT << " usecs)"
         << "  cpu secs:" << (float)(c2.secs()+(c2.usecs()/1000000.0)) / 
                                                        (float)NUMBER_REPEAT 
         << "(=" << (c2.secs()*1000000.0+c2.usecs())/NUMBER_REPEAT << " usecs)"
         << endl << endl;
    return(EXIT_SUCCESS);
}