rtxClock.h

00001 /*
00002  * Copyright (c) 1997-2010 Objective Systems, Inc.
00003  *
00004  * This software is furnished under a license and may be used and copied
00005  * only in accordance with the terms of such license and with the
00006  * inclusion of the above copyright notice. This software or any other
00007  * copies thereof may not be provided or otherwise made available to any
00008  * other person. No title to and ownership of the software is hereby
00009  * transferred.
00010  *
00011  * The information in this software is subject to change without notice
00012  * and should not be construed as a commitment by Objective Systems, Inc.
00013  *
00014  * PROPRIETARY NOTICE
00015  *
00016  * This software is an unpublished work subject to a confidentiality agreement
00017  * and is protected by copyright and trade secret law.  Unauthorized copying,
00018  * redistribution or other use of this work is prohibited.
00019  *
00020  * The above notice of copyright on this source code product does not indicate
00021  * any actual or intended publication of such source code.
00022  *
00023  *****************************************************************************/
00024 
00025 /* Defines a means of performing timing tests.  The macros may be used as
00026  * follows:
00027  *
00028  * DECLARE_BENCHMARK
00029  *
00030  * ...
00031  *
00032  * BEGIN_LOOP
00033  *    // code to be timed goes here
00034  * END_LOOP
00035  *
00036  * PRINT_RESULTS_MS
00037  *
00038  * Convenience macros to start and stop the performance clock are also
00039  * provided in case a loop is not necessary.  In this case, the ITERCNT
00040  * value should be redefined to the number of cases being tested.
00041  *
00042  * These timers should be reasonable accurate within microsecond resolution,
00043  * but results will vary from platform to platform.  On GNU/Linux systems,
00044  * it is necessary to link against librt.a.
00045  *
00046  * If a program is terminated unexpectedly, results may fail to print since
00047  * the output buffer may not be flushed.  If exception handling is used,
00048  * make sure to call fflush(stdout) or the appropriate call for your
00049  * platform. */
00050 
00051 #ifndef __RTXCLOCK_H__
00052 #define __RTXCLOCK_H__
00053 
00054 #include <cstdio>
00055 
00056 #define ITERCNT 100000L
00057 
00058 #ifndef MSC_VER      /* not MSVC++; does this work for Borland? */
00059 
00060 #include <time.h>
00061 
00062 #define DECLARE_BENCHMARK \
00063    timespec tp0, tp1; \
00064    long __clk__ = 0;
00065 
00066 #define CLOCK_START \
00067    clock_gettime(CLOCK_REALTIME, &tp0);
00068 
00069 #define CLOCK_STOP \
00070    clock_gettime(CLOCK_REALTIME, &tp1);
00071 
00072 #define BEGIN_LOOP \
00073    CLOCK_START \
00074    for (__clk__ = 0; __clk__ < ITERCNT; __clk__ ++) {
00075 
00076 #define END_LOOP \
00077    } \
00078    CLOCK_STOP
00079 
00080 #define PRINT_RESULTS_MS \
00081    long _ds = tp1.tv_sec - tp0.tv_sec, \
00082         _dn = tp1.tv_nsec - tp0.tv_nsec; \
00083    double _dms = (_ds * 1000.f) + (_dn / 1e6); \
00084    printf ("\t%.6f\t%.6f\n", _dms, _dms/(float)ITERCNT);
00085 
00086 #else                /* MSVC++ */
00087 
00088 #include <windows.h>
00089 
00090 #define DECLARE_BENCHMARK \
00091    LARGE_INTEGER _start, _stop, _freq; \
00092    long __clk__ = 0; \
00093    QueryPerformanceFrequency(&_freq);
00094 
00095 #define CLOCK_START \
00096    QueryPerformanceCounter(&_start);
00097 
00098 #define CLOCK_STOP \
00099    QueryPerformanceCounter(&_stop);
00100 
00101 #define BEGIN_LOOP \
00102    CLOCK_START \
00103    for (__clk__ = 0; __clk__ < ITERCNT; __clk__ ++) {
00104 
00105 #define END_LOOP \
00106    } \
00107    CLOCK_END
00108 
00109 #define PRINT_RESULTS_MS \
00110    double _delta = (_stop.QuadPart - _start.QuadPart), \
00111           _total = _delta / (double)_freq.QuadPart * 1000; \
00112    printf("\t%.6f\t%.6f\n", _total, _total/(float)ITERCNT);
00113 
00114 #endif
00115 
00116 #endif /* DEFINE __RTXCLOCK_H__ */
00117