All pastes #942645 Raw Edit

Stuff

public text v1 · immutable
·published 2008-03-14 17:54 UTC
rendered paste body
#include <stdio.h>

#include "profile.h"

clock_t _count;
clock_t _count_base;
clock_t _counting;

static void waste_some_time()
{
        double a, b, c;
        int i;

        a = 10; b = -3; c = 5;

        for( i = 0; i < 1000000; i++ ) {
                a = b + c;
                b = a - c;
                c = a + b;
        }
}

int main( void )
{
        prof_clear();
        prof_start();

        waste_some_time();

        prof_stop();
        prof_print();

        return 0;
}

==================================================
profile.h
/*
 * Note: codes who are using the "profile.h" header file should define
 *       the variables _count, _cont_base, and _counting !
 */

#ifndef __PROFILE_H_
#define __PROFILE_H_

#include <time.h>

extern clock_t _count;
extern clock_t _count_base;
extern clock_t _counting;

#define prof_clear()                                            \
{                                                               \
        prof_update();                                          \
        _count = 0;                                             \
        _count_base = clock();                                  \
}

#define prof_print()                                            \
{                                                               \
        printf( "CPU cycles so far: %.3f\n",                    \
                (float)_count / CLOCKS_PER_SEC );               \
}

#define prof_start()                                            \
{                                                               \
        prof_update();                                          \
        _counting = 1;                                          \
        _count_base = clock();                                  \
}

#define prof_stop()                                             \
{                                                               \
        prof_update();                                          \
        _counting = 0;                                          \
        _count_base = clock();                                  \
}

#define prof_update()                                           \
{                                                               \
        if( _counting ) {                                       \
                clock_t end = clock();                          \
                _count += end - _count_base;                    \
        }                                                       \
}

#endif