📄 csdn_文档中心_c++的profile辅助模板类 升级版.htm
字号:
color=#ffffff>标题</FONT></TD>
<TD><B> C++的profile辅助模板类
升级版</B> coolcch(原作) </TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
<TD width=500> C++ profile template</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
<TABLE border=0 width=600>
<TBODY>
<TR>
<TD align=left><BR>
<P>(如果没看过"C++的profile辅助模板类" 先去看看那里的说明部分<BR><A
href="http://www.csdn.net/Develop/read_article.asp?id=26029">http://www.csdn.net/Develop/read_article.asp?id=26029</A>
)</P>
<P>先看例子:<BR> PROCEDURE_TIMEX(1, "Sleep(200)",
Sleep(200));<BR> for (char i = 0; i != 5; ++i)<BR>
{<BR> TOTAL_COUNTX(2,
"for");<BR> TOTAL_TIMEX(3,
"for");<BR> rt = FUNC_TIMEX(4, "MySleep(200)",
MySleep(200));<BR> }<BR>运行完后结果:<BR>4 "MySleep(200)": 5 times,
1.00 seconds.<BR>3 "for": 5 times, 1.00 seconds.<BR>2 "for": 5
times.<BR>1 "Sleep(200)": 1 times, 0.20 seconds.<BR>还是很方便吧?</P>
<P>标志(就是这些宏的第一个参数)<BR>还是只能为非负整数<BR>用相同的数做标志
计时/计数结果叠加(TOTAL_COUNT只与TOTAL_COUNT叠加)<BR>出结果时
这些宏的第二个参数会显示在标志的后面<BR>其实偷懒一点 第二个参数可以不用引号引起来 如<BR>
PROCEDURE_TIMEX(1, Sleep(200), Sleep(200));<BR>但那样代码会看起来比较乱</P>
<P>另外 看了 PROCEDURE_TIME 或 PROCEDURE_TIMEX
的代码你就会发现<BR>其实可以这样用:<BR> PROCEDURE_TIMEX(1, Several
statements, <BR> {<BR> TOTAL_COUNTX(2,
"for");<BR> TOTAL_TIMEX(3,
"for");<BR> rt = FUNC_TIMEX(4, "MySleep(200)",
MySleep(200));<BR> }<BR> );</P>
<P>注意:<BR>1. 如果你前后两次用了同一个标志 但显示的字符串设的不同<BR>最后显示结果时
以最后一次设的字符串为准<BR>在实现时我用静态变量初始化的方法 这样基本上去掉了额外开销<BR>(要注意静态变量只初始化一次 所以
到底哪里才是最后一次设的要想想清楚)<BR>如:<BR> for (int i = 0; ;
++i)<BR> {<BR> PROCEDURE_TIMEX(1, "Sleep(200)",
Sleep(200));<BR> if (i == 1) break;<BR> FUNC_TIMEX(1,
"MySleep(200)", MySleep(200));<BR> }<BR>运行完后结果是:<BR>1
"MySleep(200)": 3 times, 0.60 seconds.</P>
<P>2. 如果一个标志在很多地方用
你想要把那些初始化的开销也去掉<BR>就用后面不带X的那个版本<BR>然后在别的地方用类似<BR>SET_COUNT_NAME(1,
"for count only")<BR>或<BR>SET_TIMER_NAME(1, "for else")</P>
<P><BR>再次祝大家用得愉快!</P>
<P><BR>以下代码用 VC6 GCC 编译运行通过<BR></P><PRE><FONT color=#006600>//filename: ShowRunTime.h</FONT><FONT color=#000000>
<FONT color=#0000ff>#ifndef</FONT> SHOWTIME_H
<FONT color=#0000ff>#define</FONT> SHOWTIME_H
<FONT color=#0000ff>#include</FONT> <FONT color=#000099><</FONT>stdio.h<FONT color=#000099>></FONT>
<FONT color=#0000ff>#include</FONT> <FONT color=#000099><</FONT>windows.h<FONT color=#000099>></FONT>
<FONT color=#0000ff>#include</FONT> <FONT color=#000099><</FONT>time.h<FONT color=#000099>></FONT>
<FONT color=#0000ff>template</FONT> <FONT color=#000099><</FONT><FONT color=#0000ff>int</FONT> I<FONT color=#000099>></FONT>
<FONT color=#0000ff>class</FONT> CShowRunTimesAss
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>public</FONT><FONT color=#000099>:</FONT>
<FONT color=#0000ff>unsigned</FONT> <FONT color=#0000ff>int</FONT> total_times<FONT color=#000099>;</FONT>
CShowRunTimesAss<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
total_times <FONT color=#000099>=</FONT> <FONT color=#6600ff>0</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#000099>~</FONT>CShowRunTimesAss<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>if</FONT> <FONT color=#000099>(</FONT>total_times <FONT color=#000099>=</FONT><FONT color=#000099>=</FONT> <FONT color=#000099>-</FONT><FONT color=#6600ff>1</FONT><FONT color=#000099>)</FONT>
<FONT color=#0000ff>return</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>char</FONT> tmp<FONT color=#000099>[</FONT><FONT color=#6600ff>256</FONT><FONT color=#000099>]</FONT><FONT color=#000099>;</FONT>
sprintf<FONT color=#000099>(</FONT>tmp<FONT color=#000099>,</FONT> <FONT color=#ff00ff>"\n%d %s: %u times.\n"</FONT><FONT color=#000099>,</FONT> I<FONT color=#000099>,</FONT> GetName<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>,</FONT> total_times<FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
printf<FONT color=#000099>(</FONT>tmp<FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
OutputDebugString<FONT color=#000099>(</FONT>tmp<FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> AddShowRunTimes<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>static</FONT> CShowRunTimesAss<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT> tt<FONT color=#000099>;</FONT>
<FONT color=#000099>+</FONT><FONT color=#000099>+</FONT>tt.total_times<FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>char</FONT><FONT color=#000099>*</FONT> GetName<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>char</FONT> str<FONT color=#000099>[</FONT><FONT color=#6600ff>128</FONT><FONT color=#000099>]</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>return</FONT> str<FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
CShowRunTimesAss<FONT color=#000099>(</FONT><FONT color=#0000ff>const</FONT> <FONT color=#0000ff>char</FONT><FONT color=#000099>*</FONT> <FONT color=#0000ff>const</FONT> str<FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
total_times <FONT color=#000099>=</FONT> <FONT color=#000099>-</FONT><FONT color=#6600ff>1</FONT><FONT color=#000099>;</FONT>
strncpy<FONT color=#000099>(</FONT>GetName<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>,</FONT> str<FONT color=#000099>,</FONT> <FONT color=#6600ff>127</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#000099>}</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>template</FONT> <FONT color=#000099><</FONT><FONT color=#0000ff>int</FONT> I<FONT color=#000099>></FONT>
<FONT color=#0000ff>class</FONT> CShowRunTimeAss
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>public</FONT><FONT color=#000099>:</FONT>
<FONT color=#0000ff>unsigned</FONT> <FONT color=#0000ff>int</FONT> total_time<FONT color=#000099>;</FONT>
<FONT color=#0000ff>unsigned</FONT> <FONT color=#0000ff>int</FONT> total_times<FONT color=#000099>;</FONT>
CShowRunTimeAss<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
total_time <FONT color=#000099>=</FONT> total_times <FONT color=#000099>=</FONT> <FONT color=#6600ff>0</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#000099>~</FONT>CShowRunTimeAss<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>if</FONT> <FONT color=#000099>(</FONT>total_times <FONT color=#000099>=</FONT><FONT color=#000099>=</FONT> <FONT color=#000099>-</FONT><FONT color=#6600ff>1</FONT><FONT color=#000099>)</FONT>
<FONT color=#0000ff>return</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>char</FONT> tmp<FONT color=#000099>[</FONT><FONT color=#6600ff>256</FONT><FONT color=#000099>]</FONT><FONT color=#000099>;</FONT>
sprintf<FONT color=#000099>(</FONT>tmp<FONT color=#000099>,</FONT> <FONT color=#ff00ff>"\n%d %s: %u times, %4.2f seconds.\n"</FONT><FONT color=#000099>,</FONT>
I<FONT color=#000099>,</FONT> GetName<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>,</FONT> total_times<FONT color=#000099>,</FONT> <FONT color=#000099>(</FONT><FONT color=#0000ff>double</FONT><FONT color=#000099>)</FONT>total_time <FONT color=#000099>/</FONT> CLOCKS_PER_SEC<FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
printf<FONT color=#000099>(</FONT>tmp<FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
OutputDebugString<FONT color=#000099>(</FONT>tmp<FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>char</FONT><FONT color=#000099>*</FONT> GetName<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>char</FONT> str<FONT color=#000099>[</FONT><FONT color=#6600ff>128</FONT><FONT color=#000099>]</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>return</FONT> str<FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
CShowRunTimeAss<FONT color=#000099>(</FONT><FONT color=#0000ff>const</FONT> <FONT color=#0000ff>char</FONT><FONT color=#000099>*</FONT> <FONT color=#0000ff>const</FONT> str<FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
total_time <FONT color=#000099>=</FONT> total_times <FONT color=#000099>=</FONT> <FONT color=#000099>-</FONT><FONT color=#6600ff>1</FONT><FONT color=#000099>;</FONT>
strncpy<FONT color=#000099>(</FONT>GetName<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>,</FONT> str<FONT color=#000099>,</FONT> <FONT color=#6600ff>127</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#000099>}</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>template</FONT> <FONT color=#000099><</FONT><FONT color=#0000ff>int</FONT> I<FONT color=#000099>></FONT>
<FONT color=#0000ff>class</FONT> CShowRunTime
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>public</FONT><FONT color=#000099>:</FONT>
CShowRunTime<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
ShowRunTimeBegin<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#000099>~</FONT>CShowRunTime<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
ShowRunTimeEnd<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>static</FONT> CShowRunTimeAss<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT><FONT color=#000099>&</FONT> Gettt<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>static</FONT> CShowRunTimeAss<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT> tt<FONT color=#000099>;</FONT>
<FONT color=#0000ff>return</FONT> tt<FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>bool</FONT> ShowRunTimeBegin<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#006600>//Gettt().total_time -= clock(); // more slowly</FONT>
Gettt<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>.total_time <FONT color=#000099>-</FONT><FONT color=#000099>=</FONT> GetTickCount<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>return</FONT> <FONT color=#0000ff>true</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>template</FONT> <FONT color=#000099><</FONT><FONT color=#0000ff>typename</FONT> T<FONT color=#000099>></FONT>
<FONT color=#0000ff>static</FONT> T ShowRunTimeEnd<FONT color=#000099>(</FONT>T t<FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#006600>//Gettt().total_time += clock();</FONT>
Gettt<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>.total_time <FONT color=#000099>+</FONT><FONT color=#000099>=</FONT> GetTickCount<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>+</FONT><FONT color=#000099>+</FONT>Gettt<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>.total_times<FONT color=#000099>;</FONT>
<FONT color=#0000ff>return</FONT> t<FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>bool</FONT> ShowRunTimeBegin<FONT color=#000099>(</FONT><FONT color=#0000ff>const</FONT> <FONT color=#0000ff>char</FONT><FONT color=#000099>*</FONT> <FONT color=#0000ff>const</FONT> str<FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#0000ff>static</FONT> CShowRunTimeAss<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT> t<FONT color=#000099>(</FONT>str<FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#006600>//Gettt().total_time -= clock();</FONT>
Gettt<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>.total_time <FONT color=#000099>-</FONT><FONT color=#000099>=</FONT> GetTickCount<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#0000ff>return</FONT> <FONT color=#0000ff>true</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> ShowRunTimeEnd<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#000099>{</FONT>
<FONT color=#006600>//Gettt().total_time += clock();</FONT>
Gettt<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>.total_time <FONT color=#000099>+</FONT><FONT color=#000099>=</FONT> GetTickCount<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT>
<FONT color=#000099>+</FONT><FONT color=#000099>+</FONT>Gettt<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>.total_times<FONT color=#000099>;</FONT>
<FONT color=#000099>}</FONT>
<FONT color=#000099>}</FONT><FONT color=#000099>;</FONT>
<FONT color=#006600>//////////////////////////////////////////////////////////////////////////</FONT>
<FONT color=#0000ff>#define</FONT> TOTAL_TIME_BEGIN<FONT color=#000099>(</FONT>I<FONT color=#000099>)</FONT> \
CShowRunTime<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT><FONT color=#000099>:</FONT><FONT color=#000099>:</FONT>ShowRunTimeBegin<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT>
<FONT color=#0000ff>#define</FONT> TOTAL_TIME_END<FONT color=#000099>(</FONT>I<FONT color=#000099>,</FONT> X<FONT color=#000099>)</FONT> \
CShowRunTime<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT><FONT color=#000099>:</FONT><FONT color=#000099>:</FONT>ShowRunTimeEnd<FONT color=#000099>(</FONT>X<FONT color=#000099>)</FONT>
<FONT color=#0000ff>#define</FONT> TOTAL_COUNT<FONT color=#000099>(</FONT>I<FONT color=#000099>)</FONT> \
<FONT color=#0000ff>do</FONT> <FONT color=#000099>{</FONT> CShowRunTimesAss<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT><FONT color=#000099>:</FONT><FONT color=#000099>:</FONT>AddShowRunTimes<FONT color=#000099>(</FONT><FONT color=#000099>)</FONT><FONT color=#000099>;</FONT> <FONT color=#000099>}</FONT> <FONT color=#0000ff>while</FONT> <FONT color=#000099>(</FONT><FONT color=#6600ff>0</FONT><FONT color=#000099>)</FONT>
<FONT color=#0000ff>#define</FONT> TOTAL_TIME<FONT color=#000099>(</FONT>I<FONT color=#000099>)</FONT> \
CShowRunTime<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT> _ShowRunTime_##I
<FONT color=#0000ff>#define</FONT> PROCEDURE_TIME<FONT color=#000099>(</FONT>I<FONT color=#000099>,</FONT> X<FONT color=#000099>)</FONT> \
<FONT color=#0000ff>do</FONT> <FONT color=#000099>{</FONT> CShowRunTime<FONT color=#000099><</FONT>I<FONT color=#000099>></FONT> _ShowRunTime_<FONT color=#000099>;</FONT> X<FONT color=#000099>;</FONT> <FONT color=#000099>}</FONT> <FONT color=#0000ff>while</FONT> <FONT color=#000099>(</FONT><FONT color=#6600ff>0</FONT><FONT color=#000099>)</FONT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -