home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xco212p.zip / SAMPLES / DHRY / timedhry.mod < prev   
Text File  |  1995-12-27  |  8KB  |  270 lines

  1. MODULE timedhry;
  2.  
  3. IMPORT dry1, dry2, InOut, SysClock;
  4. FROM Storage IMPORT ALLOCATE;
  5.  
  6. (* time unit should be 1 millisecond *)
  7.  
  8. TYPE longint = CARDINAL;
  9.  
  10. VAR
  11.   BeginTime, EndTime: CARDINAL;
  12.   UserTime: longint;
  13.  
  14. PROCEDURE time(): CARDINAL;
  15.   VAR dt: SysClock.DateTime;
  16. BEGIN
  17.   SysClock.GetClock(dt);
  18.   RETURN ORD(dt.second)+60*(ORD(dt.minute)+60*ORD(dt.hour));
  19. END time;
  20.  
  21. (* InOut replacement *)
  22. PROCEDURE wl; BEGIN InOut.WriteLn; END wl;
  23. PROCEDURE wc (c: CHAR); BEGIN InOut.Write (c); END wc;
  24.  
  25. PROCEDURE ws (s: ARRAY OF CHAR);
  26. VAR i: CARDINAL;
  27. BEGIN
  28.   FOR i := 0 TO HIGH(s) DO
  29.     IF s[i]=0C THEN RETURN; END;
  30.     wc (s[i]);
  31.   END;
  32. END ws;
  33.  
  34. PROCEDURE wb (b: BOOLEAN);
  35. BEGIN
  36.   IF b THEN InOut.WriteString ("TRUE");
  37.   ELSE      InOut.WriteString ("FALSE");
  38.   END;
  39. END wb;
  40. PROCEDURE we (e: dry2.Enumeration);
  41. BEGIN
  42.   CASE e OF
  43.     |dry2.Ident1: ws ("Ident1");
  44.     |dry2.Ident2: ws ("Ident2");
  45.     |dry2.Ident3: ws ("Ident3");
  46.     |dry2.Ident4: ws ("Ident4");
  47.     |dry2.Ident5: ws ("Ident5");
  48.     |ELSE ws ("Error - unexpected value");
  49.   END;
  50. END we;
  51.  
  52. PROCEDURE wli (n: longint);
  53. VAR i: INTEGER;
  54. VAR buf: ARRAY [0..31] OF CHAR;
  55. BEGIN
  56.   i := 0;
  57.   WHILE n > 0 DO
  58.     buf[i] := CHR(ORD('0') + VAL(CARDINAL,n MOD 10)); INC(i); n := n DIV 10;
  59.   END;
  60.   IF i = 0 THEN buf[0] := '0'; INC(i); END;
  61.   REPEAT DEC(i); wc (buf[i]); UNTIL i=0;
  62. END wli;
  63.  
  64. PROCEDURE wi (i: dry2.integer);
  65. BEGIN
  66.   wli (VAL(longint, i));
  67. END wi;
  68.  
  69. PROCEDURE rli (VAR n: longint);
  70. VAR
  71.   c: CHAR;
  72.   t: longint;
  73. BEGIN
  74.   n := 0;
  75.   LOOP
  76.     InOut.Read (c);
  77.     IF (c<'0') OR (c>'9') THEN RETURN END;
  78.     t := VAL (longint, ORD(c) - ORD('0'));
  79.     IF (MAX(longint)-t) DIV 10 < n THEN
  80.       wl; wl; ws ("Error: value is too big"); wl; wl; HALT;
  81.     END;
  82.     n := n*10 + t;
  83.   END;
  84. END rli;
  85.  
  86. (* Divide with rounding (for time mesurements) *)
  87. PROCEDURE div (x, y: longint): longint;
  88. BEGIN
  89.   RETURN ((x + y DIV 2) DIV y);
  90. END div;
  91.  
  92. PROCEDURE Proc0;
  93.  
  94.   (* main program, corresponds to procedures        *)
  95.   (* Main and Proc0 in the Ada version              *)
  96.  
  97. VAR
  98.  
  99.   Int1Loc,
  100.   Int2Loc,
  101.   Int3Loc: dry2.integer;
  102.   ChIndex: CHAR;
  103.   EnumLoc: dry2.Enumeration;
  104.   Str1Loc: dry2.Str30;
  105.   Str2Loc: dry2.Str30;
  106.   RunIndex,
  107.   NumberOfRuns,
  108.   n: longint;
  109.  
  110. BEGIN
  111.  
  112.   (* Initializations *)
  113.  
  114.   NEW(dry2.NextPtrGlob);
  115.   NEW(dry2.PtrGlob);
  116.  
  117.   dry2.PtrGlob^.PtrComp  := dry2.NextPtrGlob;
  118.   dry2.PtrGlob^.Discr    := dry2.Ident1;
  119.   dry2.PtrGlob^.EnumComp := dry2.Ident3;
  120.   dry2.PtrGlob^.IntComp  := 40;
  121.   dry2.PtrGlob^.StrComp  := "DHRYSTONE PROGRAM, SOME STRING";
  122.   Str1Loc                := "DHRYSTONE PROGRAM, 1'ST STRING";
  123.  
  124.   dry2.Arr2Glob [8][7] := 10;
  125.         (* Was missing in published program. Without this statement,    *)
  126.         (* Arr2Glob [8][7] would have an undefined value.               *)
  127.         (* Warning: With 16-Bit processors and NumberOfRuns > 32000,    *)
  128.         (* overflow may occur for this array element.                   *)
  129.  
  130.   wl;
  131.   ws ("Dhrystone Benchmark, Version 2.1 (Language: Modula-2)"); wl; wl;
  132.  
  133.   ws ("Please give the number of runs through the benchmark: ");
  134.   rli (n); wl; NumberOfRuns := n;
  135.  
  136.   ws ("Execution starts, "); wli (NumberOfRuns);
  137.   ws (" runs through Dhrystone"); wl;
  138.  
  139.   RunIndex := 0;
  140.  
  141.   (***************)
  142.   (* Start timer *)
  143.   (***************)
  144.  
  145.   BeginTime:=time();
  146.  
  147.   WHILE RunIndex < NumberOfRuns DO INC (RunIndex);
  148.     dry1.Proc5;
  149.     dry1.Proc4;
  150.       (* Ch1Glob == 'A', Ch2Glob == 'B', BoolGlob == true *)
  151.     Int1Loc := 2;
  152.     Int2Loc := 3;
  153.     Str2Loc := "DHRYSTONE PROGRAM, 2'ND STRING";
  154.     EnumLoc := dry2.Ident2;
  155.     dry2.BoolGlob := NOT dry2.Func2 (Str1Loc, Str2Loc);
  156.       (* BoolGlob == 1 *)
  157.  
  158.     WHILE Int1Loc < Int2Loc DO  (* loop body executed once *)
  159.       Int3Loc := 5 * Int1Loc - Int2Loc;
  160.         (* Int3Loc == 7 *)
  161.       dry2.Proc7 (Int1Loc, Int2Loc, Int3Loc);
  162.         (* Int3Loc == 7 *)
  163.       INC (Int1Loc);
  164.     END;
  165.       (* Int1Loc == 3, Int2Loc == 3, Int3Loc == 7 *)
  166.     dry2.Proc8 (dry2.Arr1Glob, dry2.Arr2Glob, Int1Loc, Int3Loc);
  167.       (* IntGlob == 5 *)
  168.     dry1.Proc1 (dry2.PtrGlob);
  169.     FOR ChIndex := 'A' TO dry2.Ch2Glob DO
  170.         (* loop body executed twice *)
  171.       IF EnumLoc = dry2.Func1 (ChIndex, 'C') THEN
  172.           (* then, not executed *)
  173.         dry2.Proc6 (dry2.Ident1, EnumLoc);
  174.         Str2Loc := "DHRYSTONE PROGRAM, 3'RD STRING";
  175.         Int2Loc := Int1Loc;
  176.         dry2.IntGlob := Int1Loc;
  177.        END;
  178.     END;
  179.       (* Int1Loc == 3, Int2Loc == 3, Int3Loc == 7 *)
  180.     Int2Loc := Int2Loc * Int1Loc;
  181.     Int1Loc := Int2Loc DIV Int3Loc;
  182.     Int2Loc := 7 * (Int2Loc - Int3Loc) - Int1Loc;
  183.       (* Int1Loc == 1, Int2Loc == 13, Int3Loc == 7 *)
  184.     dry1.Proc2 (Int1Loc);
  185.       (* Int1Loc == 5 *)
  186.   END; (* loop "for RunIndex" *)
  187.  
  188.   (**************)
  189.   (* Stop timer *)
  190.   (**************)
  191.  
  192.   EndTime:=time();
  193.   UserTime := (EndTime-BeginTime)*1000;
  194.  
  195.   ws ("Execution ends"); wl; wl;
  196.   ws ("Final values of the variables used in the benchmark:"); wl; wl;
  197.   ws ("IntGlob:           "); wi(dry2.IntGlob); wl;
  198.   ws ("        should be: 5"); wl;
  199.   ws ("BoolGlob:          "); wb (dry2.BoolGlob); wl;
  200.   ws ("        should be: TRUE"); wl;
  201.   ws ("Ch1Glob:           "); wc (dry2.Ch1Glob); wl;
  202.   ws ("        should be: A"); wl;
  203.   ws ("Ch2Glob:           "); wc (dry2.Ch2Glob); wl;
  204.   ws ("        should be: B"); wl;
  205.   ws ("Arr1Glob[8]:       "); wi (dry2.Arr1Glob[8]); wl;
  206.   ws ("        should be: 7"); wl;
  207.   ws ("Arr2Glob[8][7]:    "); wi (dry2.Arr2Glob[8][7]); wl;
  208.   ws ("        should be: NumberOfRuns + 10"); wl;
  209.  
  210.   ws ("PtrGlob^.PtrComp must be equal to dry2.NextPtrGlob^.PtrComp -- ");
  211.   IF dry2.PtrGlob^.PtrComp=dry2.NextPtrGlob^.PtrComp THEN ws ("OK");
  212.   ELSE ws ("error!");
  213.   END;
  214.   wl;
  215.  
  216.   ws ("PtrGlob"); wl;
  217.   ws ("  Discr:           "); we (dry2.PtrGlob^.Discr); wl;
  218.   ws ("        should be: Ident1"); wl;
  219.   ws ("  EnumComp:        "); we (dry2.PtrGlob^.EnumComp); wl;
  220.   ws ("        should be: Ident3"); wl;
  221.   ws ("  IntComp:         "); wi (dry2.PtrGlob^.IntComp); wl;
  222.   ws ("        should be: 17"); wl;
  223.   ws ("  StrComp:         "); ws (dry2.PtrGlob^.StrComp); wl;
  224.   ws ("        should be: DHRYSTONE PROGRAM, SOME STRING"); wl;
  225.   ws ("NextPtrGlob"); wl;
  226.   ws ("  Discr:           "); we (dry2.NextPtrGlob^.Discr); wl;
  227.   ws ("        should be: Indent1"); wl;
  228.   ws ("  EnumComp:        "); we (dry2.NextPtrGlob^.EnumComp); wl;
  229.   ws ("        should be: Ident2"); wl;
  230.   ws ("  IntComp:         "); wi (dry2.NextPtrGlob^.IntComp); wl;
  231.   ws ("        should be: 18"); wl;
  232.   ws ("  StrComp:         "); ws (dry2.NextPtrGlob^.StrComp); wl;
  233.   ws ("        should be: DHRYSTONE PROGRAM, SOME STRING"); wl;
  234.   ws ("Int1Loc:           "); wi (Int1Loc); wl;
  235.   ws ("        should be: 5"); wl;
  236.   ws ("Int2Loc:           "); wi (Int2Loc); wl;
  237.   ws ("        should be: 13"); wl;
  238.   ws ("Int3Loc:           "); wi (Int3Loc); wl;
  239.   ws ("        should be: 7"); wl;
  240.   ws ("EnumLoc:           "); we (EnumLoc); wl;
  241.   ws ("        should be: Ident2"); wl;
  242.   ws ("Str1Loc:           "); ws (Str1Loc); wl;
  243.   ws ("        should be: DHRYSTONE PROGRAM, 1'ST STRING"); wl;
  244.   ws ("Str2Loc:           "); ws (Str2Loc); wl;
  245.   ws ("        should be: DHRYSTONE PROGRAM, 2'ND STRING"); wl; wl;
  246.  
  247.   IF UserTime < 2000 THEN
  248.     ws ("Measured time too small to obtain meaningful results"); wl;
  249.     ws ("Please increase number of runs");
  250.   ELSE
  251.     ws ("Milliseconds per "); wli (NumberOfRuns);
  252.     ws (" runs through Dhrystone: "); wli (UserTime); wl; wl;
  253.     ws ("Microseconds for one run through Dhrystone: ");
  254.     n := div(UserTime*10,div(NumberOfRuns,1000));
  255.     wli (n DIV 10); wc ('.'); wli (n MOD 10); wl;
  256.     ws ("Dhrystones per Second:                      ");
  257.     (* check the range *)
  258.     WHILE NumberOfRuns >= (MAX(longint) DIV 1000) DO
  259.       NumberOfRuns := div(NumberOfRuns,10); UserTime := div(UserTime,10);
  260.     END;
  261.     wli (div(NumberOfRuns*1000,UserTime));
  262.   END;
  263.   wl; wl;
  264. END Proc0;
  265.  
  266.  
  267. BEGIN
  268.   Proc0;
  269. END timedhry.
  270.