home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / pascal / 8058 < prev    next >
Encoding:
Internet Message Format  |  1993-01-12  |  2.5 KB

  1. Path: sparky!uunet!usc!cs.utexas.edu!asuvax!ennews!telesys!wierius!witsend!dcs
  2. Message-ID: <765355d2523469t258@witsend.uucp>
  3. Date: Monday, 11 January 1993 06:50:20 MST
  4. X-Mailer: TMail version 1.16R
  5. From: "D. C. Sessions" <dcs@witsend.tnet.com>
  6. Organization: Nobody but me -- really
  7. References: <mitchell.11@odie.ee.wits.ac.za> <1993Jan9.140610.28269@lugb.latrobe.edu.au> <11625@uqcspe.cs.uq.oz.au>
  8. Subject: Re: Faster Pascal
  9. Newsgroups: comp.lang.pascal
  10. Distribution: world
  11. Lines: 51
  12.  
  13. In <11625@uqcspe.cs.uq.oz.au>, muzzle@cs.uq.oz.au (Murray Chapman)  wrote:
  14. # In <1993Jan9.140610.28269@lugb.latrobe.edu.au> cscmd@lux.latrobe.edu.au (Mitch Davis) writes:
  15. # >In article <mitchell.11@odie.ee.wits.ac.za> mitchell@odie.ee.wits.ac.za (MITCHELL JAMES) writes:
  16. # >>Bascially, I'm trying to find a way to run Pascal code faster. I have access 
  17. # >>to a 486 but this is woefully inadequate (for my needs at least).
  18. # >I've had that problem, and I've solved it.  90% of the time, you can
  19. # >triple the speed of your program by re-writing it so it is smarter.
  20.  
  21. # Also, it's pretty well-known that on average, your computer spends 90% of
  22. # its time in 10% of the program.  In other words, there is no point in
  23. # trying to optimize statements that aren't executed multiple times.
  24. # For example:
  25. # 1:    R := R * 4;
  26. # 2:    for x := 1 to 100 do
  27. # 3:      for y := 1 to 100 do
  28. # 4:          line(0, 0, Sin(X) * R, Sin(Y) * R);
  29. # There would not be much point in changing line 1 to assembler code to do two
  30. # ASL's, as the speedup would be linear, and not all that much.
  31. # On the other hand, line 4 is executed 10000 times, and calls 20000 expensive
  32. # functions.  Make a lookup table for sin/cos, and you will save yourself 20000
  33. # function calls.
  34.  
  35.   Aside from table lookup (which might not be good enough in all
  36.   instances) there's an obvious optimization you ignored: removing
  37.   loop-invariant subexpressions.  ( Sin(X) * R ) is executed in
  38.   EVERY pass in your example, even though X is only taken 100 times.
  39.   Better to pull it out:
  40.  
  41. 2A: FOR x := 1 TO 100 DO BEGIN
  42. 2B:    sx := SIN( X ) * R;
  43. 3:     FOR y := 1 TO 100 DO
  44.  
  45.   Of course, you COULD build your table dynamically:
  46.  
  47.   FOR y := 1 TO 100 DO sy[ y ] := SIN( y ) * R;
  48.   FOR x := 1 TO 100 DO BEGIN
  49.     sx := SIN( x ) * R;
  50.     FOR x := 1 TO 100 DO
  51.       FOR y := 1 TO 100 DO
  52.         line( 0, 0, sx, sy[ y ] );
  53.  
  54. --- D. C. Sessions                            Speaking for myself ---
  55. --- Note new network address:                dcs@witsend.tnet.com ---
  56. --- Author (and everything else!) of TMail  (DOS mail/news shell) ---
  57.