Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site lanl-a.UUCP Path: utzoo!linus!decvax!ittvax!dcdwest!sdcsvax!sdcrdcf!hplabs!hao!seismo!cmcl2!lanl-a!jlg From: jlg@lanl-a.UUCP Newsgroups: net.lang Subject: FORTH efficiency Message-ID: <9623@lanl-a.UUCP> Date: Wed, 27-Jun-84 17:31:33 EDT Article-I.D.: lanl-a.9623 Posted: Wed Jun 27 17:31:33 1984 Date-Received: Sat, 30-Jun-84 07:11:56 EDT References: <289@harvard.ARPA>, <2224@mit-eddie.UUCP> Organization: Los Alamos National Laboratory Lines: 40 Does anyone know of a FORTH compiler which does ANY optimization on the generated code? Contrary to the claims about FORTH, it's been my experience that threaded programming languages are quite slow compared to proceedural languages. Most of this problem is caused by redundant stack manipulation when 'words' are invoked. For example, if I want to do the equivalent of the Fortran expression (A+B)*C in FORTH, I do "@A @B + @C *" (it's been a long time, so please forgive if the syntax is slightly wrong). This causes values of A and B to be pushed on the stack, then '+' pops them off and adds them - pushing the result back on the stack. Then the value of C get pushed on, and finally '*' pops both args off and multiplies. So the total memory activity is 5 pushes and 4 pops. In Fortran, the values of A and B are loaded, the add goes, the value of C is loaded, the multiply goes, and the result is stored - 3 loads, one store. FORTH also had to do the 3 loads and one store in addition to the stack manipulation, so that's 9 stack operations which were redundant. I don't know of any FORTH implementation which does better than this. Claims that FORTH is real efficient seem to be unfounded when compared to good compiled code from other languages. FORTH does seem to produce very compact code, and once you get used to it, it's fairly easy to write. But, to be efficient, the compiler must unthread the code and compactness is lost. Even advanced forms of FORTH in which the basic math operations were optimized suffer from this problem since all user defined 'words' are treated as external calls. This prevents peephole optimization to eliminate redundant loads and stores, as well as introducing call and return instructions where they aren't needed. This would be like writing Pascal with every statement being a proceedure call! Even the control structures in FORTH such as conditionals and loop control are usually treated as externals. This analysis may be a little old since I haven't used FORTH for some time. If there are versions of FORTH in which these problems are corrected, I'd like to know. Otherwise, please don't keep up the pretense that FORTH is faster than other languages. J.L.Giles Los Alamos Nat'l Lab ...!cmcl2!lanl-a!jlg