home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / CDEMO.ZIP / MYTEST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-06-18  |  3.2 KB  |  90 lines

  1. {
  2.   MyTest.Pas by Richard S. Sadowsky
  3.   Released to the public domain
  4.   6/18/88, Los Angeles, California
  5.  
  6.   This program tests two similar routines.  One is written in Turbo C,
  7.   assembled to asm, and doctored to assemble to a TP4 ready OBJ file,
  8.   and the other routine is the same algorithm implemented in Turbo
  9.   Pascal.  This illustrates that a significant speed advantage can
  10.   be obtained by using C for certain algorithms.
  11.  
  12.   NOTE: This code is compiled with RANGE and STACK checking turned off.
  13.         This is only fair, since the Turbo C does not check for this.
  14.  
  15.   See the files:
  16.     MYTEST.PAS - This program
  17.     MY.C       - The Turbo C routine
  18.     MY.ASM     - The ASM output generated by TCC (see MY.C for more info)
  19.     MY_P.ASM   - ASM file doctored for use with Turbo Pascal
  20.     MY_P.OBJ   - The masm generated OBJ file for use with $L
  21.     MYTEST.EXE - A precompiled example of this program
  22.     READ.ME    - A readme file to point you in the right direction
  23.  
  24.   To recompile and do this yourself, you will need Turbo Pascal version 4,
  25.   Turbo C version 1 or 1.5 (I used 1.5), and a MASM compatable assembler
  26.   (I used MASM 5.1).  If you not have these, I have included the precompiled
  27.   MYTEST.EXE, so you can see the speed difference for yourself.
  28.  
  29.   This program, MYTEST.PAS, prompts the user for a string.  The string
  30.   is then converted to uppercase 5000 times, first with the C routine
  31.   then with the Pascal routine.  The times for the process are shown and
  32.   compared for each routine.
  33. }
  34. {$R-,S-,I-}
  35. program MyTest;
  36.  
  37. uses TPDOS; { needed for TimeMS routine, although any timer will do }
  38.  
  39. {$F+} {since the C routine is declared FAR, so is the Pascal routine }
  40.  
  41. procedure StrUpr(var S : String); External; { written in C in file MY.C }
  42. {$L MY_P.obj}
  43.  
  44. procedure P_StrUpr(var S : String); { same algorithm in pascal }
  45. { this is functionally and logically equivelent to the C routine }
  46. { StrUpr from the file My.C. }
  47. var
  48.   Len,Counter : Integer;
  49.   C : Char;
  50.  
  51. begin
  52.   Len := Length(S);
  53.   for Counter := 1 to Len do begin
  54.     C := S[Counter];
  55.     if ((Ord(C) >= Ord('a')) and (Ord(C) <= Ord('z'))) then
  56.       S[Counter] := Chr(Ord(C) - 32)
  57.     else
  58.       S[Counter] := C;
  59.   end;
  60. end;
  61.  
  62. var
  63.   S,SS : String;
  64.   T1,T2,TP,TC : LongInt;
  65.   I : Word;
  66.  
  67. begin
  68.   Write('Enter a string : ');
  69.   ReadLn(S);                  { get a string to uppercase from user }
  70.   T1 := TimeMS; { starting time for C routine }
  71.   for I := 1 to 5000 do begin
  72.     SS := S;
  73.     StrUpr(SS); { upper case the string 5000 times using C routine }
  74.   end;
  75.   T2 := TimeMS; { stopping time for C routine }
  76.   WriteLn(SS);  { show the uppercase string once }
  77.   TC := T2 - T1; { calculate number of milliseconds for C routine }
  78.   WriteLn('TC time = ',TC);
  79.   T1 := TimeMS; { starting time for pascal routine }
  80.   for I := 1 to 5000 do begin
  81.     SS := S;
  82.     P_StrUpr(SS); { upper case the string 5000 times using Pascal routine }
  83.   end;
  84.   T2 := TimeMS; { stopping time for pascal routine }
  85.   WriteLn(SS);  { show the uppercase string once }
  86.   TP := T2 - T1; { calculate number of milliseconds for pascal routine }
  87.   WriteLn('TP time = ',TP);
  88.   WriteLn('Difference is ',TP - TC); {show difference (TC routine is faster)}
  89. end.
  90.