home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj8908.zip / LADD.LST < prev    next >
File List  |  1989-07-06  |  16KB  |  781 lines

  1. BENCHMARKING TURBO C AND QUICKC
  2. by Scott Robert Ladd
  3.  
  4. [GRIND.C]
  5.  
  6. /*
  7.      Program:  Grind
  8.  
  9.      Version:  1.11
  10.      Date:     26-Oct-1988
  11.  
  12.      Language: ANSI C
  13.  
  14.      Tests all aspects of a C compiler's functions, including disk i/o,
  15.      screen i/o, floating point, recursion, prototyping, and memory
  16.      allocation. It should be a large enough program to test the advanced
  17.      optimizers in some compilers.
  18.  
  19.      Developed by Scott Robert Ladd. This program is public domain.
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25.  
  26. #define MAXFLOATS 1000
  27.  
  28. struct tabent
  29.      {
  30.      double val, vsum, vsqr, vcalc;
  31.      };
  32.  
  33. struct tabent table[MAXFLOATS];
  34.  
  35. char *errmsg[] = {
  36.                  "GRIND.TBL cannot be created",
  37.                  "GRIND.TBL cannot be closed properly",
  38.                  "GRIND.IN cannot be found",
  39.                  "GRIND.IN has been truncated",
  40.                  "GRIND.IN cannot be closed properly"
  41.                  };
  42.  
  43. /* function prototypes */
  44. short main(void);
  45. void  readfloats(void);
  46. void  sortfloats(void);
  47. void  quicksort(struct tabent *, short, short);
  48. void  maketable(void);
  49. void  writetable(void);
  50. void  error(short);
  51.  
  52. short main(void)
  53.      {
  54.      puts("\nGrind (C) v1.10  -- A Benchmark Program\n");
  55.  
  56.      readfloats();
  57.  
  58.      sortfloats();
  59.  
  60.      maketable();
  61.  
  62.      writetable();
  63.  
  64.      puts("\7End run GRIND!!!");
  65.  
  66.      return(0);
  67.      }
  68.  
  69. void readfloats()
  70.      {
  71.      register short i;
  72.      char buf[12];
  73.      FILE *fltsin;
  74.  
  75.      printf("--> Reading in floats. At #     ");
  76.      if (NULL == (fltsin = fopen("GRIND.IN","r")))
  77.           error(2);
  78.  
  79.      for (i = 0; i < MAXFLOATS; ++i)
  80.           {
  81.           printf("\b\b\b\b\b%5d",i);
  82.           if (NULL == fgets(buf,12,fltsin))
  83.                error(3);
  84.           table[i].val = atof(buf);
  85.           }
  86.  
  87.      if (fclose(fltsin))
  88.           error(4);
  89.  
  90.      printf("\n");
  91.      }
  92.  
  93. void sortfloats()
  94.      {
  95.      puts("--> Sorting data");
  96.      quicksort(table,0,MAXFLOATS-1);
  97.      }
  98.  
  99. void quicksort(struct tabent * item,
  100.                short left,
  101.                short right)
  102.      {
  103.      register short i, j;
  104.      struct tabent x, y;
  105.  
  106.      i = left;
  107.      j = right;
  108.      x = item[(i+j)/2];
  109.  
  110.      do
  111.           {
  112.           while (item[i].val < x.val && i < right) i++;
  113.           while (x.val < item[j].val && j > left)  j--;
  114.           if (i <= j)
  115.                {
  116.                y = item[i];
  117.                item[i] = item[j];
  118.                item[j] = y;
  119.                i++;
  120.                j--;
  121.                }
  122.           }
  123.      while (i <= j);
  124.  
  125.      if (left < j)  quicksort(item,left,j);
  126.      if (i < right) quicksort(item,i,right);
  127.      }
  128.  
  129. void maketable()
  130.      {
  131.      register short i;
  132.      double sum = 0.0;
  133.  
  134.      puts("--> Calculating table values");
  135.  
  136.      for (i = 0; i < MAXFLOATS; ++i)
  137.           {
  138.           sum = sum + table[i].val;
  139.           table[i].vsum  = sum;
  140.           table[i].vsqr  = table[i].val * table[i].val;
  141.           table[i].vcalc = sqrt(fabs(table[i].val)) * log10(fabs(table[i].val));
  142.           }
  143.      }
  144.  
  145. void writetable()
  146.      {
  147.      FILE *fd;
  148.      register short i;
  149.  
  150.      if (NULL == (fd = fopen("GRIND.TBL","w+")))
  151.           error(0);
  152.  
  153.      puts("--> Writing Table to File");
  154.  
  155.      for (i = 0; i < MAXFLOATS; i = i + 10)
  156.           {
  157.           fprintf(fd,
  158.                   "val = %5.2f, sum = %5.2f, sqr = %5.2f, calc = %5.2f\n",
  159.                   table[i].val,
  160.                   table[i].vsum,
  161.                   table[i].vsqr,
  162.                   table[i].vcalc);
  163.           }
  164.  
  165.      if (fclose(fd))
  166.           error(1);
  167.  
  168.      }
  169.  
  170. void error(short err_no)
  171.      {
  172.      printf("\n\7GRIND ERROR: %s\n",errmsg[err_no]);
  173.      exit(err_no);
  174.      }
  175.  
  176. [DMATH.C]
  177.  
  178. /*
  179.      Benchmark: DMath
  180.      Version:   1.00     23-Jan-1989
  181.  
  182.      Language:  ANSI C
  183.  
  184.      Computes all of the sines of the angles between 0 and 360 degrees using
  185.      doubles.
  186.  
  187.      Developed by Scott Robert Ladd. This program is public domain.
  188. */
  189.  
  190. #define dabs(a) (((a) < 0.0) ? (-(a)) : (a))
  191.  
  192. /* conversion factor for converting radian to degrees */
  193.  
  194. #define deg2rad 57.29577951
  195.  
  196. /* prototypes */
  197. void main(void);
  198. double fact(double);
  199. double power(double, double);
  200.  
  201. void main()
  202.     {
  203.     double angle, radians, sine, worksine, temp, k;
  204.  
  205.     for (angle = 0.0; angle <= 360.0; angle += 1.0)
  206.         {
  207.         radians = angle / deg2rad;
  208.         k = 0.0;
  209.         worksine = 0.0;
  210.  
  211.         do  {
  212.             sine = worksine;
  213.             temp = (2.0 * k) + 1.0;
  214.             worksine += (power(-1.0,k) / fact(temp)) * power(radians,temp);
  215.             k += 1.0;
  216.             }
  217.         while (dabs(sine - worksine) > 1E-8);
  218.         }
  219.     }
  220.  
  221. /* Note: this function is designed for speed; it ONLY works when n is integral */
  222. double fact(double n)
  223.     {
  224.     double res;
  225.  
  226.     res = 1.0;
  227.  
  228.     while (n > 0.0)
  229.         {
  230.         res *= n;
  231.         n -= 1.0;
  232.         }
  233.  
  234.     return res;
  235.     }
  236.  
  237. /* Note: this function is designed for speed; it ONLY works when p is integral */
  238. double power(double n, double p)
  239.     {
  240.     double res;
  241.  
  242.     res = 1.0;
  243.  
  244.     while (p > 0.0)
  245.         {
  246.         res *= n;
  247.         p -= 1.0;
  248.         }
  249.  
  250.     return res;
  251.     }
  252.  
  253.  
  254. [FXREF.C]
  255.  
  256. /*
  257.      Program:  FXREF (File Cross-Reference)
  258.      
  259.      Version:  1.10
  260.      Date:     21-Sep-1988
  261.      
  262.      Language: ANSI C
  263.      
  264.      Reads a file from standard input, and sorts and organizes each
  265.      token (word) found using a binary tree, keeping track of the number
  266.      of occurences of each token and their location by line and column.
  267.      It then prints a report to stdout.
  268.      
  269.      Released into the public domain for "educational" purposes.
  270. */
  271.  
  272. #include "stdio.h"
  273. #include "string.h"
  274. #include "ctype.h"
  275. #include "stdlib.h"
  276.  
  277. /* type definitions */
  278. typedef unsigned short line_no;
  279.  
  280. typedef struct loc_s
  281.      {
  282.      line_no line;
  283.      struct loc_s * next;
  284.      }
  285.      location;
  286.  
  287. typedef struct tok_s
  288.      {
  289.      struct tok_s * less, * more;
  290.      char * text;
  291.      struct loc_s *loc, *last;
  292.      }
  293.      token;
  294.  
  295. token * root;
  296.  
  297. char * err_msg[] = {
  298.                   "token table root",
  299.                   "token text",
  300.                   "location references",
  301.                   "token record"
  302.                   };
  303.  
  304. /* function prototypes */
  305. int main(void);
  306. void parse_tokens(char *, line_no);
  307. void add_tree(char *, line_no);
  308. token * find_tree(char *);
  309. void show_tree(token *);
  310. void error(short);
  311.  
  312. int main()
  313.      {
  314.      char buf[256];
  315.      line_no line=0;
  316.  
  317.      if (NULL == (root = ( token *)malloc(sizeof( token))))
  318.           error(0);
  319.  
  320.      root->less = NULL;
  321.      root->more = NULL;
  322.      root->text = NULL;
  323.      root->loc  = NULL;
  324.  
  325.      while (NULL != (fgets(buf,256,stdin)))
  326.           {
  327.           ++line;
  328.           printf("%5u: %s",line,buf);
  329.           parse_tokens(buf,line);
  330.           }
  331.           
  332.      printf("\x0C\n");
  333.  
  334.      show_tree(root);
  335.  
  336.      return 0;
  337.      }
  338.      
  339. void parse_tokens(char * buf, line_no line)
  340.      {
  341.      char tok[256];
  342.      line_no pos;
  343.      
  344.      while (1)
  345.           {
  346.           while ((!isalpha(*buf)) && (*buf != 0))
  347.                ++buf;
  348.           if (*buf == 0)
  349.                return;
  350.           pos = 0;
  351.           while (isalpha(*buf))
  352.                tok[pos++] = *buf++;
  353.           tok[pos] = 0;
  354.           add_tree(tok,line);
  355.           }
  356.      }
  357.  
  358. void add_tree(char * tok, line_no line)
  359.      {
  360.      token *temp_tok, *new_tok;
  361.      location *temp_loc;
  362.      short comp;
  363.      
  364.      if (root->text == NULL)
  365.           {
  366.           if (NULL == (root->text = (char *)malloc((unsigned)strlen(tok)+1)))
  367.                error(1);
  368.           strcpy(root->text,tok);
  369.           if (NULL == (root->loc = ( location *)malloc(sizeof( location))))
  370.                error(2);
  371.           root->loc->line = line;
  372.           root->loc->next = NULL;
  373.           root->last = root->loc;
  374.           return;
  375.           }
  376.           
  377.      temp_tok = find_tree(tok);
  378.      
  379.      if (comp = strcmp(tok,temp_tok->text))
  380.           /* comp is true (non-zero) if they don't match */
  381.           {
  382.           if (NULL == (new_tok = ( token *)malloc(sizeof( token))))
  383.                error(3);
  384.           if (NULL == (new_tok->text = (char *)malloc((unsigned)strlen(tok)+1)))
  385.                error(1);
  386.           new_tok->less = NULL;
  387.           new_tok->more = NULL;
  388.           strcpy(new_tok->text,tok);
  389.           if (NULL == (new_tok->loc = ( location *)malloc(sizeof( location))))
  390.                error(2);
  391.           new_tok->loc->line = line;
  392.           new_tok->loc->next = NULL;
  393.           new_tok->last = new_tok->loc;
  394.           if (comp < 0)
  395.                temp_tok->less = new_tok;
  396.             else
  397.                temp_tok->more = new_tok;
  398.           }
  399.        else
  400.           /* if comp is false (0), the tokens match */
  401.           {
  402.           if (NULL == (temp_loc = ( location *)malloc(sizeof( location))))
  403.                error(2);
  404.           temp_loc->line = line;
  405.           temp_loc->next = NULL;
  406.           temp_tok->last->next = temp_loc;
  407.           temp_tok->last = temp_loc;
  408.           }
  409.      }
  410.  
  411.  token *find_tree(char * tok)
  412.      {
  413.      short comp;
  414.      token *node;
  415.      
  416.      node = root;
  417.      
  418.      while (1)
  419.           {
  420.           if (0 == (comp = strcmp(tok,node->text)))
  421.                return node;
  422.           if (comp < 0)
  423.                if (node->less == NULL)
  424.                     return node;
  425.                  else
  426.                     node = node->less;
  427.             else
  428.                if (node->more == NULL)
  429.                     return node;
  430.                  else
  431.                     node = node->more;
  432.           }
  433.      }
  434.  
  435. void show_tree(token * node)
  436.      {
  437.      location *lloc;
  438.      short pos;
  439.      
  440.      if (NULL == node) return;
  441.      
  442.      show_tree(node->less);
  443.      printf("%-32s: ",node->text);
  444.      pos = -1;
  445.      lloc = node->loc;
  446.      while (lloc != NULL)
  447.           {
  448.           if (++pos == 7)
  449.                {
  450.                pos = 0;
  451.                printf("\n%32s: "," ");
  452.                }
  453.           printf("%5d ",lloc->line);
  454.           lloc = lloc->next;
  455.           }
  456.      printf("\n");
  457.      show_tree(node->more);
  458.      }
  459.  
  460. void error(short err_no)
  461.      {
  462.      printf("\nFXREF ERROR: Cannot allocate space for %s\n",err_msg[err_no]);
  463.      exit(err_no+1);
  464.      }
  465.  
  466. [GRAPH_QC.C]
  467.  
  468. /*
  469.     Benchmark:  GRAPH_QC
  470.     Version:    1.00    29-Jan-1989
  471.  
  472.     Language:   Microsoft QuickC v2.00
  473.  
  474.     Purpose:    Tests the speed of QuickC's graphics functions.
  475.  
  476.     Written by Scott Robert Ladd. Released into the public domain.
  477. */
  478.  
  479. #include "time.h"
  480. #include "stdio.h"
  481. #include "graph.h"
  482. #include "stddef.h"
  483.  
  484. short main(void);
  485. void initialize(void);
  486. void draw_lines(void);
  487. void draw_ellipses(void);
  488. void draw_and_fill(void);
  489. void finalize(void);
  490.  
  491. short max_x, max_y;
  492. short mid_x, mid_y;
  493.  
  494. struct videoconfig scrn_cfg ;
  495.  
  496. clock_t start;
  497. float   line_time, ellipse_time, fill_time;
  498.  
  499. short main()
  500.     {
  501.     initialize();
  502.  
  503.     draw_lines();
  504.     draw_ellipses();
  505.     draw_and_fill();
  506.  
  507.     finalize();
  508.  
  509.     return 0;
  510.     }
  511.  
  512. void initialize()
  513.     {
  514.     _setvideomode(_ERESCOLOR);
  515.  
  516.     _getvideoconfig(&scrn_cfg);
  517.  
  518.     max_x = scrn_cfg.numxpixels - 1;
  519.     max_y = scrn_cfg.numypixels - 1;
  520.  
  521.     mid_x = max_x / 2;
  522.     mid_y = max_y / 2;
  523.     }
  524.  
  525. void draw_lines()
  526.     {
  527.     short i, x, y;
  528.  
  529.     start = clock();
  530.  
  531.     for (i = 0; i <= 5; ++i)
  532.         {
  533.         if ((i % 2) == 0)
  534.             _setcolor(_BRIGHTWHITE);
  535.         else
  536.             _setcolor(_BLACK);
  537.  
  538.         for (x = 0; x <= max_x; x += 4)
  539.             {
  540.             _moveto(x,0);
  541.             _lineto(max_x - x,max_y);
  542.             }
  543.  
  544.         for (y = 0; y <= max_y; y += 2)
  545.             {
  546.             _moveto(max_x,y);
  547.             _lineto(0,max_y - y);
  548.             }
  549.         }
  550.  
  551.     line_time = (clock() - start) / CLK_TCK;
  552.  
  553.     _clearscreen(_GCLEARSCREEN);
  554.     }
  555.  
  556. void draw_ellipses()
  557.     {
  558.     short x, y;
  559.  
  560.     _setcolor(_BRIGHTWHITE);
  561.  
  562.     start = clock();
  563.  
  564.     for (x = 6; x < mid_x; x += 6)
  565.         for (y = 10; y < mid_y; y += 10)
  566.             _ellipse(_GBORDER, mid_x - x, mid_y - y, mid_x + x, mid_y + y);
  567.  
  568.     ellipse_time = (clock() - start) / CLK_TCK;
  569.  
  570.     _clearscreen(_GCLEARSCREEN);
  571.     }
  572.  
  573. void draw_and_fill()
  574.     {
  575.     short i, color;
  576.  
  577.     _moveto(0,0);
  578.  
  579.     _lineto(20,0);
  580.     _lineto(30,20);
  581.     _lineto(10,40);
  582.     _lineto(10,50);
  583.     _lineto(100,50);
  584.     _lineto(100,52);
  585.     _lineto(50,52);
  586.     _lineto(50,54);
  587.     _lineto(102,54);
  588.     _lineto(102,10);
  589.     _lineto(630,120);
  590.     _lineto(500,150);
  591.     _lineto(620,180);
  592.     _lineto(510,200);
  593.     _lineto(630,250);
  594.     _lineto(400,340);
  595.     _lineto(5,300);
  596.     _lineto(0,0);
  597.  
  598.     _setfillmask(NULL);
  599.  
  600.     start = clock();
  601.  
  602.     for (i = 0; i < 4; ++i)
  603.         {
  604.         for (color = 1; color < 15; ++color)
  605.             {
  606.             _setcolor(color);
  607.             _floodfill(mid_x, mid_y, _BRIGHTWHITE);
  608.             }
  609.         }
  610.  
  611.     fill_time = (clock() - start) / CLK_TCK;
  612.  
  613.     _clearscreen(_GCLEARSCREEN);
  614.     }
  615.  
  616. void finalize()
  617.     {
  618.     _setvideomode(_DEFAULTMODE);
  619.  
  620.     printf("line    time = %.1f\n",line_time);
  621.     printf("ellipse time = %.1f\n",ellipse_time);
  622.     printf("fill    time = %.1f\n",fill_time);
  623.     }
  624.  
  625.  
  626. [GRAPH_TC.C]
  627.  
  628. /*
  629.     Benchmark:  GRAPH_TC
  630.     Version:    1.00    29-Jan-1989
  631.  
  632.     Language:   Borland Turbo C v2.0
  633.  
  634.     Purpose:    Tests the speed of QuickC's graphics functions.
  635.  
  636.     Written by Scott Robert Ladd. Released into the public domain.
  637. */
  638.  
  639. #include "time.h"
  640. #include "stdio.h"
  641. #include "graphics.h"
  642.  
  643. short main(void);
  644. void initialize(void);
  645. void draw_lines(void);
  646. void draw_ellipses(void);
  647. void draw_and_fill(void);
  648. void finalize(void);
  649.  
  650. short max_x, max_y;
  651. short mid_x, mid_y;
  652.  
  653. clock_t start;
  654. float   line_time, ellipse_time, fill_time;
  655.  
  656. int driver = EGA,
  657.     mode = EGAHI;
  658.  
  659. short main()
  660.     {
  661.     initialize();
  662.  
  663.     draw_lines();
  664.     draw_ellipses();
  665.     draw_and_fill();
  666.  
  667.     finalize();
  668.  
  669.     return 0;
  670.     }
  671.  
  672. void initialize()
  673.     {
  674.     initgraph(&driver, &mode, "D:\\TC\\BGI");
  675.  
  676.     max_x = 639;
  677.     max_y = 349;
  678.  
  679.     mid_x = max_x / 2;
  680.     mid_y = max_y / 2;
  681.     }
  682.  
  683. void draw_lines()
  684.     {
  685.     short i, x, y;
  686.  
  687.     start = clock();
  688.  
  689.     for (i = 0; i <= 5; ++i)
  690.         {
  691.         if ((i % 2) == 0)
  692.             setcolor(WHITE);
  693.         else
  694.             setcolor(BLACK);
  695.  
  696.         for (x = 0; x <= max_x; x += 4)
  697.             {
  698.             moveto(x,0);
  699.             lineto(max_x - x,max_y);
  700.             }
  701.  
  702.         for (y = 0; y <= max_y; y += 2)
  703.             {
  704.             moveto(max_x,y);
  705.             lineto(0,max_y - y);
  706.             }
  707.         }
  708.  
  709.     line_time = (clock() - start) / CLK_TCK;
  710.  
  711.     cleardevice();
  712.     }
  713.  
  714. void draw_ellipses()
  715.     {
  716.     short x, y;
  717.  
  718.     setcolor(WHITE);
  719.  
  720.     start = clock();
  721.  
  722.     for (x = 6; x < mid_x; x += 6)
  723.         for (y = 10; y < mid_y; y += 10)
  724.             ellipse(mid_x, mid_y, 0, 360, x, y);
  725.  
  726.     ellipse_time = (clock() - start) / CLK_TCK;
  727.  
  728.     cleardevice();
  729.     }
  730.  
  731. void draw_and_fill()
  732.     {
  733.     short i, color;
  734.  
  735.     moveto(0,0);
  736.  
  737.     lineto(20,0);
  738.     lineto(30,20);
  739.     lineto(10,40);
  740.     lineto(10,50);
  741.     lineto(100,50);
  742.     lineto(100,52);
  743.     lineto(50,52);
  744.     lineto(50,54);
  745.     lineto(102,54);
  746.     lineto(102,10);
  747.     lineto(630,120);
  748.     lineto(500,150);
  749.     lineto(620,180);
  750.     lineto(510,200);
  751.     lineto(630,250);
  752.     lineto(400,340);
  753.     lineto(5,300);
  754.     lineto(0,0);
  755.  
  756.     start = clock();
  757.  
  758.     for (i = 0; i < 4; ++i)
  759.         {
  760.         for (color = 1; color < 15; ++color)
  761.             {
  762.             setfillstyle(SOLID_FILL,color);
  763.             floodfill(mid_x, mid_y, WHITE);
  764.             }
  765.         }
  766.  
  767.     fill_time = (clock() - start) / CLK_TCK;
  768.  
  769.     cleardevice();
  770.     }
  771.  
  772. void finalize()
  773.     {
  774.     closegraph();
  775.  
  776.     printf("line    time = %.1f\n",line_time);
  777.     printf("ellipse time = %.1f\n",ellipse_time);
  778.     printf("fill    time = %.1f\n",fill_time);
  779.     }
  780.  
  781.