home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1995 July / IMM0795.ISO / share / os2 / sysbench / src / pmb_hano.ckr < prev    next >
Text File  |  1994-11-05  |  10KB  |  448 lines

  1. /************************************************/
  2. /*  hanoi.c, Version 1.0, 18 Sep 1992  --- AAA  */
  3. /*           Version 1.1, 06 Jul 1993  --- AAA  */
  4. /*           Added print out for all disks      */
  5. /*           Version 1.2, 28 May 1994  --- AAA  */
  6. /*           Calculate average Moves/25usec     */
  7. /************************************************/
  8.  
  9. /**************************************************/
  10. /* Adapted from the MUSBUS test suite.            */
  11. /* hanoi.c, V3.5 87/08/06 08:11:14 kenj           */
  12. /*                                                */
  13. /* Various timer routines are included. Please    */
  14. /* select the appropriate timer routine for your  */
  15. /* system from the options below. If no option is */
  16. /* available then you will need to create your    */
  17. /* own similar 'hanoi_time()' timer routine.           */
  18. /*                                                */
  19. /* You can uncomment one of the timer options     */
  20. /* below to use it, or you can compile with the   */
  21. /* option name. For example you can compile with  */
  22. /* '-DUNIX', or '-DVMS', '-DMSC',..., etc.        */
  23. /*                                                */
  24. /* Example compilation:                           */
  25. /*  cc -DUNIX hanoi.c -o hanoi, or                */
  26. /* gcc -DUNIX -O hanoi.c -o hanoi, or             */
  27. /*  cc -dAmiga hanoi.c -o hanoi, ..., etc         */
  28. /**************************************************/
  29.  
  30. #include <stdio.h>
  31.  
  32. /***************************************************************/
  33. /* Timer options. You MUST uncomment one of the options below  */
  34. /* or compile, for example, with the '-DUNIX' option.          */
  35. /***************************************************************/
  36. /* #define Amiga       */
  37. /* #define UNIX        */
  38. /* #define UNIX_Old    */
  39. /* #define VMS         */
  40. /* #define BORLAND_C   */
  41. /* #define MSC         */
  42. /* #define MAC         */
  43. /* #define IPSC        */
  44. /* #define FORTRAN_SEC */
  45. /* #define GTODay      */
  46. /* #define CTimer      */
  47. /* #define UXPM        */
  48.  
  49. #define other(i,j) (6-(i+j))
  50.  
  51. static double TimeArray[3];
  52. static int num[4];
  53. static long count;
  54.  
  55. double pmb_hanoi(void)
  56. {
  57.     double RunTime = 0.0, sum_mps = 0.0, TLimit, mps;
  58.     int disk, Loops = 0;
  59.     
  60. /*    printf("\n");
  61.     printf("Towers of Hanoi Puzzle Test Programn\n");
  62.     printf("Disks     Moves     Time(sec)   Moves/25usec\n");
  63. */
  64.     TLimit  = 30.0;
  65.     disk    = 15;
  66.  
  67.     while ( RunTime < TLimit )
  68.     {
  69.      disk++;
  70.      num[0] = 0;
  71.      num[1] = disk;
  72.      num[2] = 0;
  73.      num[3] = 0;
  74.      count  = 0;
  75.  
  76.      hanoi_time(TimeArray);
  77.      mov(disk,1,3);
  78.      hanoi_time(TimeArray);
  79.      
  80.      RunTime = TimeArray[1];
  81.      mps = 2.5E-05 * ( (double)count/RunTime );
  82.      Loops = Loops + 1;
  83.      sum_mps = sum_mps + mps;
  84. //     printf("%3ld  %10ld  %12.5lf  %10.4lf\n",disk,count,RunTime,mps);
  85.      
  86.      if ( disk == 30 ) break;
  87.     }
  88.     
  89.     sum_mps = sum_mps / (double)Loops;
  90. //    printf("\nAverage Moves Per 25 usec = %10.4lf\n",sum_mps);
  91. //    printf("\n");
  92.  
  93.     return sum_mps;
  94. }
  95.  
  96. static mov(n,f,t)
  97. {
  98.    int o;
  99.    if(n == 1) 
  100.    {
  101.       num[f]--;
  102.       num[t]++;
  103.       count++;
  104.       return;
  105.    }
  106.    o = other(f,t);
  107.    mov(n-1,f,o);
  108.    mov(1,f,t);
  109.    mov(n-1,o,t);
  110.    return;
  111. }
  112.  
  113. /*****************************************************/
  114. /* Various timer routines.                           */
  115. /* Al Aburto, aburto@marlin.nosc.mil, 26 Sep 1992    */
  116. /*                                                   */
  117. /* hanoi_time(p) outputs the elapsed time seconds in p[1] */
  118. /* from a call of hanoi_time(p) to the next call of       */
  119. /* hanoi_time(p).  Use CAUTION as some of these routines  */
  120. /* will mess up when timing across the hour mark!!!  */
  121. /*                                                   */
  122. /* For timing I use the 'user' time whenever         */
  123. /* possible. Using 'user+sys' time is a separate     */
  124. /* issue.                                            */
  125. /*                                                   */
  126. /*****************************************************/
  127.  
  128. /*************************************/
  129. /* Timer code.                       */
  130. /*************************************/
  131. /*******************/
  132. /*  Amiga hanoi_time()  */
  133. /*******************/
  134. #ifdef Amiga
  135. #include <ctype.h>
  136. #define HZ 50
  137.  
  138. hanoi_time(p)
  139. double p[];
  140. {
  141.    double q;
  142.  
  143.    struct   tt {
  144.       long  days;
  145.       long  minutes;
  146.       long  ticks;
  147.    } tt;
  148.  
  149.    q = p[2];
  150.  
  151.    DateStamp(&tt);
  152.  
  153.    p[2] = ( (double)(tt.ticks + (tt.minutes * 60L * 50L)) ) / (double)HZ;
  154.    p[1] = p[2] - q;
  155.    
  156.    return 0;
  157. }
  158. #endif
  159.  
  160. /*****************************************************/
  161. /*  UNIX hanoi_time(). This is the preferred UNIX timer.  */
  162. /*  Provided by: Markku Kolkka, mk59200@cc.tut.fi    */
  163. /*  HP-UX Addition by: Bo Thide', bt@irfu.se         */
  164. /*****************************************************/
  165. #ifdef UNIX
  166. #include <sys/time.h>
  167. #include <sys/resource.h>
  168.  
  169. #ifdef hpux
  170. #include <sys/syscall.h>
  171. #define getrusage(a,b) syscall(SYS_getrusage,a,b)
  172. #endif
  173.  
  174. struct rusage rusage;
  175.  
  176. hanoi_time(p)
  177. double p[];
  178. {
  179.    double q;
  180.  
  181.    q = p[2];
  182.  
  183.    getrusage(RUSAGE_SELF,&rusage);
  184.  
  185.    p[2] = (double)(rusage.ru_utime.tv_sec);
  186.    p[2] = p[2] + (double)(rusage.ru_utime.tv_usec) * 1.0e-06;
  187.    p[1] = p[2] - q;
  188.    
  189.    return 0;
  190. }
  191. #endif
  192.  
  193. /***************************************************/
  194. /*  UNIX_Old hanoi_time(). This is the old UNIX timer.  */
  195. /*  Use only if absolutely necessary as HZ may be  */
  196. /*  ill defined on your system.                    */
  197. /***************************************************/
  198. #ifdef UNIX_Old
  199. #include <sys/types.h>
  200. #include <sys/times.h>
  201. #include <sys/param.h>
  202.  
  203. #ifndef HZ
  204. #define HZ 60
  205. #endif
  206.  
  207. struct tms tms;
  208.  
  209. hanoi_time(p)
  210. double p[];
  211. {
  212.    double q;
  213.  
  214.    q = p[2];
  215.  
  216.    times(&tms);
  217.  
  218.    p[2] = (double)(tms.tms_utime) / (double)HZ;
  219.    p[1] = p[2] - q;
  220.    
  221.    return 0;
  222. }
  223. #endif
  224.  
  225. /*********************************************************/
  226. /*  VMS hanoi_time() for VMS systems.                         */
  227. /*  Provided by: RAMO@uvphys.phys.UVic.CA                */
  228. /*  Some people have run into problems with this timer.  */
  229. /*********************************************************/
  230. #ifdef VMS
  231. #include time
  232.  
  233. #ifndef HZ
  234. #define HZ 100
  235. #endif
  236.  
  237. struct tbuffer_t
  238.        {
  239.     int proc_user_time;
  240.     int proc_system_time;
  241.     int child_user_time;
  242.     int child_system_time;
  243.        };
  244. struct tbuffer_t tms;
  245.  
  246. hanoi_time(p)
  247. double p[];
  248. {
  249.    double q;
  250.  
  251.    q = p[2];
  252.  
  253.    times(&tms);
  254.  
  255.    p[2] = (double)(tms.proc_user_time) / (double)HZ;
  256.    p[1] = p[2] - q;
  257.    
  258.    return 0;
  259. }
  260. #endif
  261.  
  262. /******************************/
  263. /*  BORLAND C hanoi_time() for DOS */
  264. /******************************/
  265. #ifdef BORLAND_C
  266. #include <ctype.h>
  267. #include <dos.h>
  268. #include <time.h>
  269.  
  270. #define HZ 100
  271. struct time tnow;
  272.  
  273. hanoi_time(p)
  274. double p[];
  275. {
  276.    double q;
  277.  
  278.    q = p[2];
  279.  
  280.    gettime(&tnow);
  281.  
  282.    p[2] = 60.0 * (double)(tnow.ti_min);
  283.    p[2] = p[2] + (double)(tnow.ti_sec);
  284.    p[2] = p[2] + (double)(tnow.ti_hund)/(double)HZ;
  285.    p[1] = p[2] - q;
  286.    
  287.    return 0;
  288. }
  289. #endif
  290.  
  291. /**************************************/
  292. /*  Microsoft C (MSC) hanoi_time() for DOS */
  293. /**************************************/
  294. #ifdef MSC
  295. #include <time.h>
  296. #include <ctype.h>
  297.  
  298. #define HZ CLK_TCK
  299. clock_t tnow;
  300.  
  301. hanoi_time(p)
  302. double p[];
  303. {
  304.    double q;
  305.  
  306.    q = p[2];
  307.  
  308.    tnow = clock();
  309.  
  310.    p[2] = (double)tnow / (double)HZ;
  311.    p[1] = p[2] - q;
  312.    
  313.    return 0;
  314. }
  315. #endif
  316.  
  317. /*************************************/
  318. /*  Macintosh (MAC) Think C hanoi_time()  */
  319. /*************************************/
  320. #ifdef MAC
  321. #include <time.h>
  322.  
  323. #define HZ 60
  324.  
  325. hanoi_time(p)
  326. double p[];
  327. {
  328.    double q;
  329.  
  330.    q = p[2];
  331.  
  332.    p[2] = (double)clock() / (double)HZ;
  333.    p[1] = p[2] - q;
  334.    
  335.    return 0;
  336. }
  337. #endif
  338.  
  339. /************************************************************/
  340. /*  iPSC/860 (IPSC) hanoi_time() for i860.                       */
  341. /*  Provided by: Dan Yergeau, yergeau@gloworm.Stanford.EDU  */
  342. /************************************************************/
  343. #ifdef IPSC
  344. extern double dclock();
  345.  
  346. hanoi_time(p)
  347. double p[];
  348. {
  349.    double q;
  350.  
  351.    q = p[2];
  352.  
  353.    p[2] = dclock();
  354.    p[1] = p[2] - q;
  355.    
  356.    return 0;
  357. }
  358. #endif
  359.  
  360. /**************************************************/
  361. /*  FORTRAN hanoi_time() for Cray type systems.        */
  362. /*  This is the preferred timer for Cray systems. */
  363. /**************************************************/
  364. #ifdef FORTRAN_SEC
  365.  
  366. fortran double second();
  367.  
  368. hanoi_time(p)
  369. double p[];
  370. {
  371.    double q,v;
  372.  
  373.    q = p[2];
  374.  
  375.    second(&v);
  376.    p[2] = v;
  377.    p[1] = p[2] - q;
  378.    
  379.    return 0;
  380. }
  381. #endif
  382.  
  383. /***********************************************************/
  384. /*  UNICOS C hanoi_time() for Cray UNICOS systems.  Don't use   */
  385. /*  unless absolutely necessary as returned time includes  */
  386. /*  'user+system' time.  Provided by: R. Mike Dority,      */
  387. /*  dority@craysea.cray.com                                */
  388. /***********************************************************/
  389. #ifdef CTimer
  390. #include <time.h>
  391.  
  392. hanoi_time(p)
  393. double p[];
  394. {
  395.    double    q;
  396.    clock_t   t;
  397.  
  398.        q = p[2];
  399.  
  400.        t = clock();
  401.  
  402.        p[2] = (double)t / (double)CLOCKS_PER_SEC;
  403.        p[1] = p[2] - q;
  404.  
  405.        return 0;
  406. }
  407. #endif
  408.  
  409. /********************************************/
  410. /* Another UNIX timer using gettimeofday(). */
  411. /* However, getrusage() is preferred.       */
  412. /********************************************/
  413. #ifdef GTODay
  414. #include <sys/time.h>
  415.  
  416. struct timeval tnow;
  417.  
  418. hanoi_time(p)
  419. double p[];
  420. {
  421.    double q;
  422.  
  423.    q = p[2];
  424.  
  425.    gettimeofday(&tnow,NULL);
  426.    p[2] = (double)tnow.tv_sec + (double)tnow.tv_usec * 1.0e-6;
  427.    p[1] = p[2] - q;
  428.  
  429.    return 0;
  430. }
  431. #endif
  432.  
  433.  
  434. #include <time.h>
  435.  
  436. static hanoi_time(p)
  437. double p[];
  438. {
  439.    double q;
  440.  
  441.    q = p[2];
  442.  
  443.    p[2] = ((double)clock())/CLOCKS_PER_SEC;
  444.    p[1] = p[2] - q;
  445.    
  446.    return 0;
  447. }
  448.