A highly efficient C Performance Counter Template
Email: larrin2002@msn.com
Homepage: www.larrin.net
Summary: In systems with high performance, simple and efficient performance counters are valuable to the performance bottlenecks in the discovery system. This article gives a simple but efficient C performance counter template. The counter given in this article is Windows version, but it is very simple to transplant to Linux.
The counter code is as follows:
//Performancecounter.h
#ifndef _larrinsdk_performance_counter_h_ # define _larrinsdk_performance_counter_h_
#include "larrinsdk.h" #include
Using namespace std;
Begin_larrin_sdk
Template
CperformanceCounter (const char * name): m_name (name), m_tickcounter (0) {}
PUBLIC:
~ CperformanceCounter () {cout << "PerformanceCounter" << m_name << ":" << m_tickcounter << Endl;}
Static CperformanceCounter & GetInstance (const char * name) {static cperformancecounter instance (name); returnial;}
Void Begincounter () {m_tmpcounter = gettickcount ();
Void endcounter () {m_tickcounter = (gettickcount () - m_tmpcounter;}};
template
~ CperformanceCounterHelper () {CperformanceCounter
#define define_counter (x) struct x;
#ifdef ENABLE_PERFORMANCE_COUNTER # define BEGIN_COUNTER (counter_name) CPerformanceCounter
#ENDIF / / _ LARRINSDK_PERFORMANCE_COUNTER_H_
test program:
1.main.cpp
#include "PerformanceCounter.h"
EXTERN VOID TEST1 ();
USING NAMESPACE LARRINSDK;
Define_counter (counter_1)
Void main () {{counter (counter_1) for (INT i = 0; i <300; i ) {Sleep (1);}}
TEST1 ();
//2.test1.cpp
#include "PerformanceCounter.h"
USING NAMESPACE LARRINSDK;
Define_counter (counter_1)
Void Test1 () {begin_counter (int i = 0; i <1000; i ) {Sleep (1);} end_counter (counter_1)}