A highly efficient C ++ Performance Counter Template

xiaoxiao2021-03-06  58

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 #include

Using namespace std;

Begin_larrin_sdk

Template class cperformancecounter {const char * m_name; unsigned long m_tmpcounter; unsigned long m_tickcounter;

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 class CPerformanceCounterHelper {const char * m_name; public: CPerformanceCounterHelper (const char * counter_name): m_name (counter_name) {CPerformanceCounter :: getInstance (m_name) .BeginCounter ();}

~ CperformanceCounterHelper () {CperformanceCounter :: getInstance (m_name) .endcounter ();}

#define define_counter (x) struct x;

#ifdef ENABLE_PERFORMANCE_COUNTER # define BEGIN_COUNTER (counter_name) CPerformanceCounter :: getInstance (#counter_name) .BeginCounter (); # define END_COUNTER (counter_name) CPerformanceCounter :: getInstance (#counter_name) .EndCounter (); # define COUNTER_HELPER (counter_name) CPerformanceCounterHelper counter _ ## counter_name (#counter_name); # else # define BEGIN_COUNTER (counter_name) #define END_COUNTER (counter_name) #endifEND_LARRIN_SDK

#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)}

转载请注明原文地址:https://www.9cbs.com/read-112938.html

New Post(0)