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