home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2677 < prev    next >
Internet Message Format  |  1991-02-06  |  3KB

  1. From: gtoal@tharr.UUCP (Graham Toal)
  2. Newsgroups: alt.sources
  3. Subject: [comp.sys.acorn] pmp - Poor Mans Profiler (UNOFFICAL version :-))
  4. Message-ID: <1734@tharr.UUCP>
  5. Date: 6 Feb 91 06:37:53 GMT
  6.  
  7. Archive-name: pmp/gthack
  8.  
  9. [Serendipitously, our mailer broke when I tried to post this earlier, so
  10. I've had a spare 10 minutes in which to 'tidy' the code up a little :-)]
  11.  
  12. This was Dan Bernstein's Poor Mans Profiler -- a utility to find out how often
  13. particular lines of your source have been executed.  My apologies to Dan for
  14. beating it to death.  It now works only on ANSI-compatible systems, like
  15. the Acorn Archimedes for which this posting is intended. (comp.sys.acorn)
  16. [We've been banned from posting these nasty long sources on c.s.a until we
  17. can forge enough votes for a comp.sources.acorn :-(]
  18.  
  19. %------------------------------------------- h.pmplib
  20. #ifndef PMPLIB_H
  21. #define PMPLIB_H 1
  22. #include <stdio.h>
  23. struct _p {long s; long l; char *f; struct _p *p;};
  24. extern struct _p *_pc; FILE *_pm(struct _p *t, FILE *fi);
  25. #define _pr(_ps) (_ps.p = _pc, _pc = &_ps)
  26. #define P do {static struct _p p = {0,__LINE__,__FILE__,0};\
  27. if (!((p.s)++)) _pr(p);} while(0)
  28. #define pmp(fn) fclose(_pm(_pc, fopen(fn,"w")))
  29. #endif
  30. %------------------------------------------- c.pmplib
  31. #include "pmplib.h"
  32. struct _p *_pc = 0;FILE *_pm(struct _p *t, FILE *fi) {return((t && fi) ?
  33.  _pm(t->p, fi), fprintf(fi,"%s, %ld: %ld\n",t->f,t->l,t->s), fi : fi);}
  34. %------------------------------------------- c.test
  35. #include "pmplib.h"
  36.  
  37. int main()
  38. {
  39.  int i;
  40.  int j;
  41.  
  42.  /* P: produce profiling information. */
  43.  /* pmp("test-pmp"): save pmp's results in test-pmp. */
  44.  
  45.  for (i = 0;i < 1000;i++)
  46.   {
  47.    P;
  48.    for (j = 0;j < 1000;j++)
  49.     {
  50.      P;
  51.      if (i % 2)
  52.        P;
  53.     }
  54.   }
  55.  pmp("test-pmp");
  56. }
  57. %------------------------------------------- sample-out
  58. c.test, 19: 1000
  59. c.test, 22: 1000000
  60. c.test, 24: 500000
  61. %------------------------------------------- README
  62. This is pmp, the poor man's profiler.
  63.  
  64. pmp gives you statement counts.
  65.  
  66. pmp has one big advantage over every other profiling system I've seen:
  67. it doesn't force optimization off. You can place profiling marks
  68. anywhere in your code; you only hurt optimization where you put the
  69. marks. So if you're sick of having your program slowed down dramatically
  70. just because the profiler sticks its nose into your inner loops, pmp
  71. should be a welcome relief.
  72.  
  73. Of course, pmp is also the smallest profiler known to man. [*now* :-)]
  74.  
  75. Other than that, it's pretty poor.
  76.  
  77. This was once pmp version 1.0, 9/13/90, by Daniel J. Bernstein.
  78. pmp is public domain.
  79.  
  80. This version is gtoal@ed.ac.uk's hack of the alt.sources posting --
  81. the timing stuff (which used SIGVTALARM) has been removed, and I've
  82. (ahem) shortened the code somewhat :-)
  83.  
  84. To use pmp, just cc test.c pmplib.c
  85. If you have an editor which lets you step through error listings (Emacs? Twin?)
  86. in synch with your source, use it to step through the test-pmp output file,
  87. otherwise merge the profile info with the source by hand (or hack up a prog
  88. to do it).
  89.  
  90.  
  91. Good luck!
  92. PS Dan - I removed a few lines to make it print the profile in forward
  93. (= reverse to you) order to support the editor-stepping feature... :-)
  94. %------------------------------------------- END
  95. -- 
  96. (* Posted from tharr.uucp - Public Access Unix - +44 (234) 261804 *)
  97.