home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_03 / 9n03107a < prev    next >
Text File  |  1991-01-15  |  4KB  |  176 lines

  1.  
  2. #include <stdio.h>
  3. #include <bios.h>
  4. #include <string.h>
  5.  
  6. #define LIMIT 70000L
  7.  
  8. #define INIT                  \
  9. long i;                  \
  10. long t = biostime(0,0)
  11.  
  12. #define TEST(x)               \
  13. INIT;                    \
  14. for(i=0; i<LIMIT; i++){  \
  15.      x;                  \
  16.      }                   \
  17. return (int)(biostime(0,0)-t)
  18.  
  19. #define Access(x,y) \
  20. printf("Accessing %s(): result = %d\n", #x,
  21.         elapsed[y]=x() )
  22.  
  23. typedef struct anything
  24.     {
  25. char field1[50],
  26.      field2[50],
  27.      field3[50],
  28.      field4[50];
  29. } anything;
  30.  
  31. anything s_object, *s_pointer=&s_object;
  32.  
  33. char *DestArray[4];
  34. char *SourceArray[4];
  35. char *access;
  36. int  j=0;
  37.  
  38. enum list { STRUCTURE, ARRAY, BOTH, EMPTY, ONELOOP,
  39.             TWOLOOPS, S_OBJECT };
  40.  
  41. int UsingS_Object(void)
  42. {
  43. TEST(     access = (char *)&s_object.field1;
  44.           access = (char *)&s_object.field2;      
  45.           access = (char *)&s_object.field3;
  46.           access = (char *)&s_object.field4;);  
  47. }
  48.  
  49. int UsingStructure(void)
  50.    {
  51.     TEST(      access = s_pointer->field1;
  52.           access = s_pointer->field2;      
  53.           access = s_pointer->field3;
  54.           access = s_pointer->field4;        );
  55.  
  56. int UsingArray(void)
  57.    {
  58. TEST(     access = SourceArray[0];
  59.           access = SourceArray[1];
  60.           access = SourceArray[2];
  61.           access = SourceArray[3];           );
  62. }
  63.  
  64. zint UsingStrucAndArrayBoth(void)
  65.    {
  66.     TEST(      access = s_pointer->field1;
  67.           access = s_pointer->field2;      
  68.           access = s_pointer->field3;
  69.           access = s_pointer->field4;       
  70.  
  71.           access = SourceArray[0];
  72.           access = SourceArray[1];
  73.           access = SourceArray[2];
  74.           access = SourceArray[3]; );
  75.  
  76.     }
  77.  
  78. int EmptyLoop(void)
  79.     {
  80. TEST( ; );
  81. }
  82.  
  83. int LoopToLoop(void)
  84.     {
  85. TEST( for(j=0; j<4; j++){ DestArray[j] =
  86.                 SourceArray[j]; } );
  87. }
  88.  
  89. int OneLoop(void)
  90. {
  91. TEST( for(j=0; j<4; j++){ access = SourceArray[j]; } );
  92. }
  93.  
  94. void analyze(int *times)
  95. {
  96. int total = times[STRUCTURE] + times[ARRAY];
  97. int difference = total - times[BOTH];
  98.  
  99. printf("\n1. Sum of time for (UsingStructure + \
  100. UsingArray) = %d\n", total );
  101.  
  102. printf("2. Sum less time for both in one loop = %d\n", 
  103.       difference );
  104. printf("3. Time to do an empty loop = %d\n",
  105.       times[EMPTY] );
  106. printf("4. Error (item 3 less item 2 should be near \
  107. zero) = %d\n", 
  108.       times[EMPTY]-difference );
  109. }
  110.  
  111. void CompareAccessTimes(void)
  112. {
  113. int elapsed[10];
  114.  
  115. Access(UsingS_Object,S_OBJECT);
  116. Access(UsingStructure,STRUCTURE);
  117. Access(UsingArray, ARRAY );
  118. Access(UsingStrucAndArrayBoth, BOTH);
  119. Access(EmptyLoop, EMPTY);
  120. Access(LoopToLoop,TWOLOOPS);
  121. Access(OneLoop,ONELOOP);
  122.  
  123. analyze(elapsed);
  124. }
  125.  
  126. void VerifyContents(void)
  127. {
  128. int i;
  129.  
  130. puts("The data to be accessed is as follows:\n");
  131.  
  132. /*        
  133. printf("The structure contains the following...\n");
  134. printf("\t%s\n\t%s\n\t%s\n\t%s\n",
  135.      s_pointer->field1,
  136.      s_pointer->field2,
  137.      s_pointer->field3,     
  138.      s_pointer->field4);
  139. */
  140. printf("\nThe array provides access to...\n");
  141. for(i=0; i<4; i++)
  142.      {
  143.      printf("\t%s\n", SourceArray[i] );
  144.      }
  145. puts("\nEach function returns the count of clock ticks."\
  146.      "The lower the better.\n);
  147. }
  148.  
  149. void main(void)
  150. {
  151.     
  152. printf("In this example, the size of a data pointer is %d,",
  153.       sizeof(char*));  
  154. printf("and the size\nof a function pointer is %d\n", 
  155.       sizeof(main) );
  156.  
  157. /*   initialize the structure */
  158.  
  159. strcpy(s_pointer->field1,"This is field one");
  160. strcpy(s_pointer->field2,"Here's the second field");
  161. strcpy(s_pointer->field3,"Field Three at your service");
  162. strcpy(s_pointer->field4,"Art was here");    
  163.  
  164. /* initialize the purportedly slower array */
  165.  
  166. SourceArray[0] = s_pointer->field1; 
  167. SourceArray[0] = s_pointer->field2;
  168. SourceArray[0] = s_pointer->field3;
  169. SourceArray[0] = s_pointer->field4;
  170.  
  171. VerifyContents();
  172.  
  173. CompareAccessTimes();
  174. }
  175.  
  176.