<HTML>
<HEAD>
<!-- this stylesheet will later on be added by lfparser automatically: -->
<style type="text/css">
<!--
  pre { font-family:monospace,Courier }
  pre.code { font-family:monospace,Courier;background-color:#aedbe8; }
  p.code { width:80%; alignment:center; background-color:#aedbe8; 
        border-style:none; border-width:medium; border-color:#aedbe8; 
        padding:0.1cm ; text-align:left }
-->
</style>
</HEAD>
<BODY>

<H1>ʹ��GProf���Ż����C/C++����</H1>
<H4>ArticleCategory:</H4>
Software Development
<H4>AuthorImage:</H4>
<IMG SRC="../../common/images2/ArnoutEngelen.png" ALT="Arnout Engelen" width="164" height="200">

<H4>TranslationInfo:</H4>
<P>original in en <A HREF="nospam:arnouten(Q)bzzt.net">Arnout Engelen</A>&nbsp; </P>
<P>en to CN<A HREF="http://r00t.icpcn.com"> С ��</A>&nbsp; </P>

<H4>AboutTheAuthor:</H4>
<P>
Arnout Engelen �Ǻ���Nijmegen��ѧ�����ϵ��ѧ����Ҳ�����簲ȫ��˾TUNIX�Ĺ�Ա���ڿ���ʱ�䣬��ϲ�����ܺʹ����������˹��
</P>

<H4>Abstract:</H4>
���Ż������ʱ��Ҫ��ס����ֵ���Ż��ĵط��Ż���û�б�Ҫ���ϼ���Сʱ���Ż�һ��ʵ����ֻ����0.04��ij���
<p>
GProf ʹ����һ���쳣�򵥵��Ƿdz���Ч�ķ������Ż�C/C++ ���򣬶����ܺ����׵�ʶ���ֵ���Ż��Ĵ��롣һ���򵥵İ�������������ʾ��GProf���ͨ��ʶ���Ż������ؼ������ݽṹ����ʵ��Ӧ���еij����3���ӵ�����ʱ�Ż���5��ġ�
<p>
��������������׷�ݵ�1982����ڱ������������ر����۴�ᣨthe SIGPLAN Symposium on Compiler Construction�����������������˸���UNIX ƽ̨�ϵ�һ����׼���ߡ�
<H4>ArticleIllustration:</H4>
<IMG SRC="../../common/images2/article371/profilingpicture.png" ALT="Profiling with GProf" HSPACE=10 width="400" height="63">

<H4>ArticleBody:</H4>

<h2>Profiling in a nutshell</h2>

�����Ҫ�����ĸ���dz��򵥣�ͨ����¼���������ĵ��úͽ���ʱ�䣬���ǿ��Լ����������������ʱ�ij���Ρ� ���ַ����������ƺ�Ҫ���Ѻܶ������������˵��ǣ�������ʵ����������Զ������ֻ��Ҫ���� gcc ����ʱ����һ������IJ���('-pg')���������������õģ��������Ѽ������Ҫ�������й����ݣ���Ȼ������'gprof'�Ը�����ķ�����Щ�����

<h2>��������: Pathalizer</h2>

��ʹ����һ����ʵ��ʹ�õij�������Ϊ���ӣ��� <a href="http://pathalizer.bzzt.net">pathalizer</a>��һ����: ��<code>event2dot</code>��һ����·�����¼��������ļ�ת��Ϊͼ�λ���dot���ļ��Ĺ��ߣ�executable which translates a pathalizer 'events' file to a graphviz 'dot' file����
<p>
�򵥵�˵������һ���ļ������ȡ�����¼���Ȼ�����Ƿֱ𱣴�Ϊͼ��(��ҳΪ�ڵ㣬�ҽ�ҳ��ҳ֮���ת����Ϊ�ߣ���Ȼ����Щͼ������Ϊһ�Ŵ��ͼ�Σ�������Ϊͼ�λ���'dot'��ʽ�ļ���

<h3>�������ʱ</h3>
�������Ǹ�����δ���Ż��ij����һ��ʱ���������ǵ�����Ҫ����ʱ�䡣���ҵļ������ʹ��<code>event2dot</code>����Դ�����������Ϊ���루���55000�����ݣ�������Ҫ���ֶ��ӣ�
<p>

<pre class="code">
real    3m36.316s
user    0m55.590s
sys     0m1.070s
</pre>

<h3>�������</h3>
Ҫʹ��gprof ����Ҫ�������ڱ����ʱ��Ҫ����'-pg' ѡ����Ǿ����������±���Դ�����£�
<pre class="code">
g++ -pg dotgen.cpp readfile.cpp main.cpp graph.cpp config.cpp -o event2dot
</pre>
<p>
�������ǿ����ٴ�����<code>event2dot</code>����ʹ������ǰ��ʹ�õIJ������ݡ�����������е�ʱ��<code>event2dot</code>���еķ������ݻᱻ�Ѽ���������'gmon.out'�ļ��У����ǿ���ͨ������'gprof <code>event2dot</code> | less'���鿴�����
<p>
gprof ����ʾ�����µĺ����Ƚ���Ҫ��
<pre class="code">
 % cumulative  self              self     total           
 time seconds  seconds  calls s/call s/call name    
43.32   46.03  46.03 339952989  0.00  0.00 CompareNodes(Node *,Node *)
25.06   72.66  26.63    55000   0.00  0.00 getNode(char *,NodeListNode *&amp;)
16.80   90.51  17.85 339433374  0.00  0.00 CompareEdges(Edge *,AnnotatedEdge *)
12.70  104.01  13.50    51987   0.00  0.00 addAnnotatedEdge(AnnotatedGraph *,Edge *)
 1.98  106.11   2.10    51987   0.00  0.00 addEdge(Graph *,Node *,Node *)
 0.07  106.18   0.07        1   0.07  0.07 FindTreshold(AnnotatedEdge *,int)
 0.06  106.24   0.06        1   0.06 28.79 getGraphFromFile(char *,NodeListNode *&amp;,Config *)
 0.02  106.26   0.02        1   0.02 77.40 summarize(GraphListNode *,Config *)
 0.00  106.26   0.00    55000   0.00  0.00 FixName(char *)
</pre>

���Կ�������һ�������Ƚ���Ҫ: ����������󲿷ֵ�����ʱ��������ռ���ˡ�

<h3>�Ż�</h3>

���������Կ������������󲿷ֵ�ʱ�䶼������<code>CompareNodes</code>�����ϣ��� grep �鿴һ������CompareNodes ֻ�DZ�<code>CompareEdges</code>������һ�ζ���, ��CompareEdges��ֻ��<code>addAnnotatedEdge</code>���á������Ƕ���������������嵥�С������������Ӧ�������Ż��ĵط��˰ɣ�

<p>
����ע�⵽<code>addAnnotatedEdge</code>������һ����������Ȼ����������ʵ�֣�����ȴʵ�ڲ�����õ��������͡����Ǿ��������� g-&gt;edges �ö�����������: �⽫��ʹ�ò��Ҹ��졣
<h3>���</h3>
�������ǿ�һ���Ż�������н��:
<pre class="code">
real    2m19.314s
user    0m36.370s
sys     0m0.940s
</pre>
<h3>�ڶ���</h3>
�ٴ����� gprof ������:
<pre class="code">
%   cumulative self           self    total           
 time   seconds seconds calls  s/call  s/call name    
87.01     25.25  25.25  55000    0.00    0.00 getNode(char *,NodeListNode *&amp;)
10.65     28.34   3.09  51987    0.00    0.00 addEdge(Graph *,Node *,Node *)
</pre>
��������ǰռ�ô�������ʱ�ĺ��������Ѿ�������ռ������ʱ�Ĵ�ͷ�ˣ�������һ�����Ż�һ���أ��ýڵ��ϣ����ȡ���ڵ�����
<p>
��μ�ֱ�Ǹ��޴�Ľ���:
<pre class="code">
real    0m3.269s
user    0m0.830s
sys     0m0.090s
</pre>

<h2>���� C/C++ ���������</h2>

<img src="../../common/images2/article371/shot.png" align="right" width="565" height="337">
���������ܶ����������ʹ��gprof ������, ����<a href="http://kprof.sf.net">KProf</a> (����) �� <a href="http://mvertes.free.fr/">cgprof</a>����Ȼͼ�ν���Ŀ���������������Ҹ�����Ϊ�����е�gprof ʹ�ø����㡣 
<br clear="all">

<h2>���������Եij�����з���</h2>

���������������gprof ����C/C++ �ij�����з�����������������ʵһ����������: �� Perl,���ǿ�����Devel::DProf ģ�顣��ij���Ӧ����<code>perl -d:DProf mycode.pl</code>����ʼ����ʹ��<code>dprofpp</code>���鿴���������������������gcj ���������Java ������Ҳ����ʹ��gprof��Ȼ��Ŀǰ��ֻ֧�ֵ��̵߳�Java ���롣

<h2>����</h2>

���������Ѿ������ģ����ǿ���ʹ�ó����Ҫ�������ٵ��ҵ�һ����������ֵ���Ż��ĵط�����ֵ���Ż��ĵط��Ż������ǿ��Խ�һ�����������ʱ�� 3��36�� ���ٵ����� 5�룬�������������ӿ�����һ����

<h2>References</h2>
<ul>
<li>Pathalizer:   <a
href="http://pathalizer.sf.net">http://pathalizer.sf.net</a><br><br>
<li>KProf:        <a
href="http://kprof.sf.net">http://kprof.sf.net</a><br><br>
<li>cgprof:       <a
href="http://mvertes.free.fr">http://mvertes.free.fr</a><br><br>
<li>Devel::DProf  <a
href="http://www.perldoc.com/perl5.8.0/lib/Devel/DProf.html">http://www.perldoc.com/perl5.8.0/lib/Devel/DProf.html</a><br><br>
<li>gcj:          <a
href="http://gcc.gnu.org/java">http://gcc.gnu.org/java</a><br><br>
<li>: pathalizer example files:         <a
href="../../common/src2/article371">download for article371</a><br><br>
</ul>
</BODY>
</HTML>
<!-- vim: set sw=2 ts=2 et tw=74: -->