home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_08 / pughcod.txt < prev    next >
Text File  |  1991-07-17  |  7KB  |  309 lines

  1.  
  2.  
  3. TIMING PROGRAM EXECUTION FROM WITHIN MAIN()!
  4.      Originally by:  A. Shipman, rewritten By:  Chris Meyer
  5.  
  6.  
  7.     In this example, the size of the data pointer is 2,
  8.  
  9. The data to be accessed is as follows:
  10.  
  11. The structure contains the following...
  12.     This is field one
  13.     Here is the second field
  14.     Field Three at your service
  15.     Chris was here
  16. The array provides access to...
  17.     This is field one
  18.     Here is the second field
  19.     Field Three at your service
  20.     Chris was here
  21.  
  22. Each function returns the count of clock ticks.The lower the better.
  23.  
  24. accessing using Object: result = 2
  25. accessing using Structure: result = 4
  26. accessing using Array: result = 3
  27. accessing using Both Structure and Array: result = 4
  28. accessing using NULL statement empty loop: result = 2
  29. Two loops Source to destination Arrays: result = 16
  30. accessing one Array Loop: result = 13
  31.  
  32. 1. Sum of time for (UsingStructure + UsingArray) = 7
  33.  
  34. 2. Sum less time for both in one loop = 3
  35.  
  36. 3. time to do an empty loop = 2
  37.  
  38. 4. Error  (item 3 less item 2 should be near zero) = 1
  39.  
  40.  
  41. Listing 5 (Shipman's )
  42.  
  43. The  measured  times,  for all the  various  Structure  and  Array  Accesses
  44. using this program, as it was written by Art Shipman, remain almost the same
  45. no  matter  which  compiler  optimization  switches  are  on  or  off.   The
  46. program simply gives an excessive timing count for each  activity  measured.
  47.  
  48.  
  49. The data to be accessed is as follows:
  50.  
  51. The structure contains the following...
  52.     This is field one
  53.     Here is the second field
  54.     Field Three at your service
  55.     Chris was here
  56. The array provides access to...
  57.     This is field one
  58.     Here is the second field
  59.     Field Three at your service
  60.     Chris was here
  61.  
  62. Each function returns the count of clock ticks.The lower the better.
  63.  
  64. accessing usings_object(): result = 4
  65. accessing usingstructure(): result = 4
  66. accessing usingarray(): result = 4
  67. accessing usingstrucandarrayboth(): result = 7
  68. accessing emptyloop(): result = 2
  69. accessing looptoloop(): result = 14
  70. accessing oneloop(): result = 13
  71.  
  72. 1. Sum of time for (UsingStructure + UsingArray) = 8
  73.  
  74. 2. Sum less time for both in one loop = 1
  75.  
  76. 3. time to do an empty loop = 2
  77.  
  78. 4. Error  (item 3 less item 2 should be near zero) = 1
  79.  
  80.  
  81. Listing5
  82.  
  83. /* #include <stdio.h> */
  84. /* #include <bios.h> */
  85. /* #include <string.h> */
  86.  
  87. #define LIMIT 70000L
  88.  
  89. /* Get current BIOS time. (t below)       */
  90. /* Return to fn. LIMIT times to do read   */
  91. /* Then get biostime - t (earlier time)   */
  92.  
  93. #define INIT long i; long t = biostime(0,0)
  94.  
  95. /*
  96. #define access(y,x)             \
  97.   printf("accessing %s(): result = %d\n", #x, elapsed[y]=x() )
  98. */
  99.  
  100. #define TEST(x)          \
  101. INIT;                    \
  102. for(i=0; i<LIMIT; i++)  {       \
  103.       x;             \
  104.   }      \
  105.   return(int)    \
  106.   (biostime(0,0)-t)
  107.  
  108. /* #x holds the name of the function that was just run ie. usings_object */
  109. /* y will hold the time that has elapsed. */
  110.  
  111. #define access(x,y)             \
  112.   printf("accessing %s(): result = %d\n", #x, elapsed[y]=x() )
  113.  
  114.  
  115. typedef struct anything
  116.      {
  117.      char field1[50],
  118.      field2[50],
  119.      field3[50],
  120.      field4[50];
  121.      } anything;
  122.  
  123. anything s_object;
  124. anything *s_pointer = &s_object;
  125.  
  126. char *destarray[4];
  127. char *sourcearray[4];
  128. char *access;
  129. int j=0;
  130.  
  131. /* Global Variables of type enum, end up as numbers, not names! */
  132. enum list
  133.     {
  134.     S_OBJECT,
  135.     STRUCTURE,
  136.     ARRAY,
  137.     BOTH,
  138.     EMPTY,
  139.     ONELOOP,
  140.     TWOLOOPS
  141.     };
  142.  
  143. void main(void)
  144. {
  145. clrscr();
  146.  
  147.        /* Initialize Structure */
  148.  
  149. strcpy(s_pointer->field1,"This is field one");
  150. strcpy(s_pointer->field2,"Here is the second field");
  151. strcpy(s_pointer->field3,"Field Three at your service");
  152. strcpy(s_pointer->field4,"Chris was here");
  153.  
  154.  
  155.        /* Initialize the purportedly slower array */
  156.  
  157. sourcearray[0] = s_pointer->field1;
  158. sourcearray[1] = s_pointer->field2;
  159. sourcearray[2] = s_pointer->field3;
  160. sourcearray[3] = s_pointer->field4;
  161.  
  162. VerifyContents();
  163.  
  164. compareaccesstimes();
  165.  
  166. }
  167.  
  168.  
  169. /* Each of the TEST functions calls the TEST prototyped in the preprocessor */
  170. /* And the arrays are accessed LIMIT amount of times before being printed.  */
  171.  
  172. int usings_object(void)
  173. {
  174.  
  175.  
  176. TEST(   access = (char *)&s_object.field1;
  177.     access = (char *)&s_object.field2;
  178.     access = (char *)&s_object.field3;
  179.     access = (char *)&s_object.field4;
  180.      );
  181. }
  182.  
  183. int usingstructure(void)
  184. {
  185. TEST(   access = s_pointer->field1;
  186.     access = s_pointer->field2;
  187.     access = s_pointer->field3;
  188.     access = s_pointer->field4;
  189.      );
  190. }
  191.  
  192.  
  193. int usingarray(void)
  194. {
  195. TEST(   access = sourcearray[0];
  196.     access = sourcearray[1];
  197.     access = sourcearray[2];
  198.     access = sourcearray[3];
  199.  
  200.     );
  201. }
  202.  
  203.  
  204. int usingstrucandarrayboth(void)
  205. {
  206. TEST(   access = s_pointer->field1;
  207.     access = s_pointer->field2;
  208.     access = s_pointer->field3;
  209.     access = s_pointer->field4;
  210.  
  211.     access = sourcearray[0];
  212.     access = sourcearray[1];
  213.     access = sourcearray[2];
  214.     access = sourcearray[3];
  215.      );
  216. }
  217.  
  218.  
  219. /* These are called toward the end of compareaccesstimes and TEST in turn, */
  220. /* Is done and results printed out behind the scene! Not viewable in DEBUG!*/
  221.  
  222. int emptyloop(void)
  223. {
  224. TEST( ; );
  225. }
  226.  
  227. int looptoloop(void)
  228. {
  229. TEST(  for(j=0; j<4; j++)
  230.        {
  231.        destarray[j] = sourcearray[j];
  232.        }
  233.     );
  234. }
  235.  
  236.  
  237.  
  238. int oneloop(void)
  239. {
  240. TEST(   for(j=0; j<4; j++)
  241.     {
  242.     access = sourcearray[j];
  243.     }
  244.      );
  245. }
  246.  
  247.  
  248.  
  249. void analyze(int *times)
  250. {
  251. int total = times[STRUCTURE] + times[ARRAY];
  252. int difference = total - times[BOTH];
  253.  
  254. printf("\n1. Sum of time for (UsingStructure + \
  255. UsingArray) = %d\n", total );
  256.  
  257. printf("\n2. Sum less time for both in one loop = %d\n", difference );
  258.  
  259. printf("\n3. time to do an empty loop = %d\n", times[EMPTY] );
  260.  
  261. printf("\n4. Error  (item 3 less item 2 should be near \
  262. zero) = %d\n",times[EMPTY]-difference );
  263.  
  264.  
  265. }
  266.  
  267.  
  268. compareaccesstimes()
  269. {
  270. int elapsed[10];                  /* Array of 10 integers */
  271. int chris = 0;
  272. access (usings_object,S_OBJECT);
  273. access (usingstructure,STRUCTURE);
  274. access (usingarray,ARRAY);
  275. access (usingstrucandarrayboth,BOTH);
  276. access (emptyloop,EMPTY);
  277. access (looptoloop,TWOLOOPS);
  278. access (oneloop,ONELOOP);
  279.  
  280. analyze(elapsed);
  281. }
  282.  
  283.  
  284.  
  285.  
  286. VerifyContents()
  287. {
  288. int i;
  289. puts("The data to be accessed is as follows:\n");
  290.  
  291.  
  292. printf("The structure contains the following...\n");
  293. printf("\t%s\n\t%s\n\t%s\n\t%s\n",
  294.       s_pointer->field1,
  295.       s_pointer->field2,
  296.       s_pointer->field3,
  297.       s_pointer->field4);
  298.  
  299. printf("The array provides access to...\n");
  300. for(i=0; i<4; i++)
  301.     {
  302.     printf("\t%s\n", sourcearray[i] );
  303.     }
  304.  
  305. puts("\nEach function returns the count of clock ticks."\
  306.     "The lower the better.\n");
  307.  
  308. }
  309.