C ++ PROFILE Auxiliary Template Upgrade

zhaozj2021-02-08  231

(If you haven't seen "C Profile Assist Template" first go to see the description of the description http://www.9cbs.net/develop/read_article.asp?id=26029)

First see example: Procedure_timex (1, "Sleep (200)", SLEEP (200)); for (char i = 0; i! = 5; i) {total_countx (2, "for"); Total_Timex (3 , "for"); rt = func_timex (4, "MySleep (200)", mysleep (200));} After running: 4 "MySleep (200)": 5 Times, 1.00 Seconds.3 "for": 5 Times, 1.00 Seconds.2 "for": 5 Times.1 "Sleep (200)": 1 Times, 0.20 seconds. Is it still very convenient?

The flag (the first parameter of these macros) is still only the result of the same number to make the same number, and the second parameter of these macros is displayed when the result is superimposed (TOTAL_COUNT only with total_count). The latitude lazy little second parameters can be caused by procatedure_timex (1, Sleep (200), Sleep (200)); but that code will look more chaotic

In addition, I will find the code of procedure_time or procedure_timex. You will find it can be used: Procedure_Timex (1, several statements, {total_countx (2, "for"); Total_Timex (3, "for"); rt = func_timex (4, " Mysleep (200) ", MySleep (200));});

Note: 1. If you use the same flag before and after the same log, the last display result of the displayed character is set, and the first time set is subject to the first time I use the static variable initialization method. Additional overhead (pay attention to the static variable only once, so where to go is the last set, you must think about it), such as: for (int i = 0; i) {procedure_timex (1, "Sleep (200)", SLEEP (200)); if (i == 1) Break; func_timex (1, "mysleep (200)", mysleep (200));} After running, the result is: 1 "Mysleep (200)": 3 Times, 0.60 seconds.

2. If a sign is in many places to use the initialization overhead, it is used to use the version that does not take X, then uses a similar set_count_name (1, "for count only") or set_timer_name (1, "in other places. "for else")

I wish you all a good job!

The following code is compiled with VC6 GCC.

// filename: Showruntime.h

#ifndef showtime_h

#define showtime_h

#include

#include

#include

Template

Class cshowruntimeSass

{

PUBLIC:

Unsigned int total_times; cshowruntimesass ()

{

Total_times = 0;

}

~ Cshowruntimesass ()

{

IF (total_times == -1)

Return;

CHAR TMP [256];

Sprintf (TMP, "/ N% D% S:% U Times./N", I, GetName (), Total_Times);

PRINTF (TMP);

OutputDebugstring (TMP);

}

Static void addshowruntimes ()

{

Static cshowruntimesss tt;

TT.TOTAL_TIMES;

}

Static char * getname ()

{

Static Char Str [128];

Return Str;

}

CShowRuntimeSass (Const Char * Const Str)

{

Total_times = -1;

STRNCPY (GetName (), STR, 127);

}

}

Template

Class cshowruntimeass

{

PUBLIC:

Unsigned int total_time;

Unsigned int total_times;

Cshowruntimeass ()

{

Total_time = Total_Times = 0;

}

~ Cshowruntimeass ()

{

IF (total_times == -1)

Return;

CHAR TMP [256];

Sprintf (TMP, "/ N% D% S:% U TIMES,% 4.2F Seconds./N",

I, getname (), total_times, (double) total_time / clocks_per_sec);

PRINTF (TMP);

OutputDebugstring (TMP);

}

Static char * getname ()

{

Static Char Str [128];

Return Str;

}

CShowRuntimeass (Const Char * Const Str)

{

Total_time = Total_Times = -1;

STRNCPY (GetName (), STR, 127);

}

}

Template

Class cshowruntime

{

PUBLIC:

Cshowruntime ()

{

Showruntimebegin ();

}

~ Cshowruntime ()

{

ShowrunTimeEnd ();

}

Static CShowRuntimeAss & gett ()

{

Static cshowruntimeass tt;

Return TT;

}

Static Bool ShowRuntimeBegin ()

{

//Gettt().total_time - = clock (); // more slowly

Gett (). Total_time - = gettickcount ();

Return True;

}

Template

Static T ShowruntimeEnd (t t)

{

//Gettt().total_time = clock ();

Gett (). Total_time = gettickcount ();

Gett (). Total_Times;

Return T;

}

Static Bool ShowruntimeBegin (const char * const str) {

Static cshowruntimeass t (str);

//Gettt().total_time - = clock ();

Gett (). Total_time - = gettickcount ();

Return True;

}

Static void showruntimend ()

{

//Gettt().total_time = clock ();

Gett (). Total_time = gettickcount ();

Gett (). Total_Times;

}

}

//

#define Total_Time_Begin (i) /

CShowRuntime :: ShowruntimeBegin ()

#define Total_Time_END (i, x) /

CshowRuntime :: ShowruntimeEnd (x)

#define Total_count (i) /

Do {cshowruntimesss :: addshowruntimes ();} while (0)

#define Total_Time (i) /

CShowRuntime _showruntime _ ## i

#define procedure_time (i, x) /

Do {cshowruntime _showruntime_; x;} while (0)

#define func_time (i, x) /

(Total_time_begin (i)? Total_time_end (i, x): total_time_end (i, x))

#define ref_func_time (i, x) /

(TOTAL_TIME_BEGIN (I)? * Total_time_end (i, & (x)): * Total_time_end (i, & (x))))

#define set_count_name (i, s) /

Static cshowruntimesss _SET_SHOWRUNTIMES_NAME _ ## i (#s)

#define set_timer_name (i, s) /

Static CShowRuntimeAss _SET_SHOWRUNTIME_NAME _ ## i (#s)

#define Total_Time_Beginx (i, s) /

CShowRuntime :: ShowruntimeBegin (#s)

#define Total_CountX (i, s) /

Do {set_count_name (i, s); cshowruntimesses :: addshowruntimes ();} while (0)

#define Total_Timex (i, s) /

Set_timer_name (i, s); cshowruntime _showruntime _ ## i

#define procedure_timex (i, s, x) /

Do {set_timer_name (i, s); cshowruntime _showruntime_; x;} while (0)

#define func_timex (i, s, x) /

(Total_Time_Beginx (i, s)? Total_time_end (i, x): total_time_end (i, x))

#define ref_func_timex (i, s, x) /

(Total_Time_Beginx (i, s)? * Total_time_end (i, & (x)): * Total_time_end (i, & (x))))

#ndif // #ifndef showtime_h

// filename: main.cpp

#include

#include "showruntime.h"

Using namespace std;

Class ctest1

{

PUBLIC:

CTEST1 (Const Ctest1 & RHS) {cout << "CTEST1 () COPY / N";

CTEST1 () {cout << "ctest1 () / n";}

~ ctest1 () {cout << "~ ctest1 () / n";

}

CTEST1 ftest1 ()

{

COUT << "ftest1 () / n";

Return ctest1 ();

}

Int testVal ()

{

Cout << "TestVal ()" << endl;

Return 0;

}

INT & TESTREF ()

{

Static Int i;

COUT << "TestRef ()" << i << endl;

Return I;

}

Const Int & TestConstref ()

{

Static Int i;

Cout << "TestConstref () << i << Endl;

Return I;

}

Int MySleep (DWORD DWMILLISECONDS)

{

Sleep (dwmilliseconds);

Return 0;

}

int main ()

{

{TOTAL_TIMEX (0, "main");

CTEST1 T;

INT RT, I;

COUT << "Call ftest1 Direct / N";

FTEST1 ();

COUT << "/ call ftest1 direct / n";

COUT << "Call ftest1 Indirect / N";

Func_timex (1, "ftest1", ftest1 ());

Cout << "/ call ftest1 indIRECT / N";

Cout << Endl;

TestRef () = 1;

// TestConstRef () = 2;

// TestVal () = 3;

REF_FUNC_TIMEX (10, "Testref", TestRef ()) = 4;

// reb_func_time (10, testconstref ()) = 5; // vc6 error!

// Should Not Be Ok. Assignment of Read-Only Location!

// reb_func_time (10, testval ()) = 6;

COUT << "Call Testref Direct / N";

Cout << "Result address is:" << (int) & testRef () << endl

COUT << "/ call testref Direct / N"; COUT << "Call Testref Indirect / N";

Cout << "Result Address IS: <<

(int) & ref_func_timex (11, "testref", testRef ()) << endl;

Cout << "/ Call Address Indirect / N";

Cout << Endl;

Procedure_timex (11, "Sleep", SLEEP (200));

For (i = 0; i! = 5; i)

{

Total_countx (2, "for");

Total_Timex (3, "for");

Rt = func_timex (4, "mysleep", mysleep (200));

}

Procedure_timex (12, for Several Statements,

{

COUT << "Call ftest1 indirect overce again / n";

Func_timex (1, "ftest1", ftest1 ());

Cout << "/ call ftest1 indirect overce again / n";

COUT << "Call ftest1 Indirect Once Again, And Use Another Counter / N";

FUNC_TIMEX (5, "ftest1", ftest1 ());

Cout << "/ call ftest1 Indirect Once Again, And Use ANother Counter / N";

}

);

For (i = 0; i)

{

Procedure_timex (13, "Sleep (200)", SLEEP (200));

IF (i == 1) Break;

FUNC_TIMEX (13, "MySleep (200)", MySleep (200));

}

} // Total_Timex (0, main);

// set_timer_name (13, "Sleep (200) mysleep (200) SLEEP (200)"); // system ("pause");

Return 0;

}

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

New Post(0)