home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctech / hlsrc / hlsort.c < prev    next >
Text File  |  1988-09-09  |  5KB  |  213 lines

  1. /*+
  2.     Name:    HLSORT.C
  3.     Author:     Kent J. Quirk
  4.         (c) Copyright 1988 Ziff Communications Co.
  5.     Date:       April 1988
  6.     Abstract:   This program tests CPU speed by generating a random array of
  7.         data and then sorting it.
  8.     History:    09-Sep-88   kjq     Version 1.00
  9. -*/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #include "hl.h"
  15. #include "hltimes.h"
  16.  
  17. #define DT_INT 0
  18. #define DT_LONG 1
  19. #define DT_FLOAT 2
  20.  
  21. /* this program can be converted to sort ints, longs, or floats simply 
  22.     by changing the following definition */
  23. #define DT_TYPE DT_INT
  24.  
  25. #if DT_TYPE == DT_INT
  26. typedef int DATATYPE;
  27. #elif DT_TYPE == DT_LONG
  28. typedef long DATATYPE;
  29. #elif DT_TYPE == DT_FLOAT
  30. typedef float DATATYPE;
  31. #endif
  32.  
  33.  
  34. void usage()
  35. {
  36.     printf("Usage:  HLSORT [-?] [-nNPTS] [-p]\n");
  37.     printf("   NPTS is the number of points in the array (default 30,000).\n");
  38.     printf("   -r reverse-sorts data after forward sort.\n");
  39.     printf("      This can take a LONG time!\n");
  40.     printf("   -p sends the data to stdout when finished.\n");
  41.     exit(1);
  42. }
  43.  
  44. /**** r a n d d a t a ****
  45.     Abstract:    Generate a random number of the appropriate type.
  46. ****************************/
  47. DATATYPE randdata()
  48. {
  49.     DATATYPE x;
  50.     x = (DATATYPE)rand();       /* change for non-numeric types */
  51.     return(x);
  52. }
  53.  
  54. /**** c o m p d a t a ****
  55.     Abstract:    Compare two data elements.
  56. ****************************/
  57. int compdata(elem1, elem2)
  58. DATATYPE *elem1, *elem2;
  59. {
  60.     return((int)(*elem1 - *elem2));     /* change for non-numeric types */
  61. }
  62.  
  63. /**** r c o m p d a t a ****
  64.     Abstract:    Compare two data elements for qsort().
  65. ****************************/
  66. int rcompdata(elem1, elem2)
  67. DATATYPE *elem1, *elem2;
  68. {
  69.     return((int)(*elem2 - *elem1));     /* change for non-numeric types */
  70. }
  71.  
  72. /**** p r i n t d a t a ****
  73.     Abstract:    Prints an item of the appropriate data type. 
  74. ****************************/
  75. void printdata(x)
  76. DATATYPE x;
  77. {
  78. #if DT_TYPE == DT_INT
  79.     printf("%d\n", x);
  80. #elif DT_TYPE == DT_LONG
  81.     printf("%ld\n", x);
  82. #elif DT_TYPE == DT_FLOAT
  83.     printf("%g\n", x);
  84. #endif
  85. }
  86.  
  87. TIME_REC timerecs[] = {
  88.     {  0L, "Total: CPU/Sort" } ,
  89.     {  0L, "Data Generation" } ,
  90.     {  0L, "Data Sort" } ,
  91.     { -1L, "HLSORT" } ,
  92. };
  93.  
  94. int main(argc, argv)
  95. int argc;
  96. char *argv[];
  97. {
  98.     int i;
  99.     DATATYPE *data;
  100.     int npts = 30000;
  101.     int printout = 0;
  102.     int reverse = 0;
  103.     int fix_microsoft = 0;
  104.     int bench = 0;
  105.     int program = -1;
  106.     char *filename = NULL;
  107.  
  108.     for (i=1; i<argc; i++)
  109.     {
  110.     if (argv[i][0] != '-')
  111.     {
  112.         printf("Invalid argument '%s'\n", argv[i]);
  113.         usage();
  114.     }
  115.  
  116.     switch(tolower(argv[i][1])) {
  117.     case 'a': 
  118.     case 'b':
  119.         bench = 1;
  120.         break;
  121.     case 'n':
  122.         npts = atoi(argv[i]+2);
  123.         break;
  124.     case 'o':
  125.         printout = 1;
  126.         break;
  127.     case 'r':
  128.         reverse = 1;
  129.         break;
  130.     case 'm':
  131.         fix_microsoft = 1;
  132.         break;
  133.     case 'p':
  134.         program = atoi(argv[i]+2);
  135.         break;
  136.     case 'f':
  137.         filename = argv[i]+2;
  138.         break;
  139.     default:
  140.         printf("Unknown argument '%s'\n", argv[i]);
  141.     case '?':                       /* default will fall through */
  142.         usage();
  143.     }
  144.     }
  145.  
  146.     if ((data = calloc(npts, sizeof(DATATYPE))) == NULL)
  147.     {
  148.     printf("Unable to alloc enough space for %d %d-byte records.\n",
  149.     npts, sizeof(DATATYPE));
  150.     usage();
  151.     }
  152.  
  153.     printf("Generating %d %d-byte records.\n", npts, sizeof(DATATYPE));
  154.  
  155.     start_timer(0);
  156.     start_timer(1);
  157.     srand(1);           /* initialize seed to known pattern */
  158.     for (i=0; i<npts; i++)
  159.     data[i] = randdata();
  160.     stop_timer();
  161.     timerecs[1].ticks = get_timer(1);
  162.  
  163.     start_timer(1);
  164.     qsort(data, npts, sizeof(DATATYPE), compdata);
  165.     stop_timer();
  166.     timerecs[2].ticks = get_timer(1);
  167.     timerecs[0].ticks = get_timer(0);
  168.  
  169.     if (reverse)
  170.     {
  171.     start_timer(1);
  172.     if (fix_microsoft)
  173.     {
  174.         DATATYPE t;
  175.         i = rand() % npts;        /* get number in range */
  176.         t = data[0];        /* and swap with first location */
  177.         data[0] = data[i];
  178.         data[i] = t;
  179.     }
  180.     qsort(data, npts, sizeof(DATATYPE), rcompdata);
  181.     stop_timer();
  182.  
  183.     printf("Reverse sort of already-sorted data took %ld ticks.\n",
  184.         get_timer(1));
  185.     }
  186.  
  187.     if (!bench)
  188.     for (i=0; timerecs[i].ticks != -1; i++)
  189.         printf("%s:  %s\n", time_secs(timerecs[i].ticks),
  190.         timerecs[i].desc);
  191.  
  192.     if ((program != -1) && (filename != NULL))
  193.     {
  194.     opentime(filename);
  195.     for (i=0; ;i++)
  196.     {
  197.         savetime(program, i, &timerecs[i]);
  198.         if (timerecs[i].ticks == -1)
  199.         break;
  200.     }
  201.     closetime();
  202.     }
  203.  
  204.     if (printout)
  205.     {
  206.     printf("Sorted data:\n");
  207.     for (i=0; i<npts; i++)
  208.         printdata(data[i]);
  209.     }
  210.     return(0);
  211. }
  212. 
  213.