Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!claris!outpost.UUCP!peirce From: peirce@outpost.UUCP (Michael Peirce) Newsgroups: comp.sys.mac.programmer Subject: Re: Pascal deficiency? Message-ID: <0B010004.o0u3z7@outpost.UUCP> Date: 19 Dec 90 01:31:59 GMT Reply-To: peirce@outpost.UUCP Organization: Peirce Software Lines: 139 X-Mailer: uAccess - Mac Release: 1.0.3 In article <1990Dec18.215406.29735@Neon.Stanford.EDU>, philip@pescadero.Stanford.EDU (Philip Machanick) writes: > > In article , sandy@snoopy.cs.umass.edu (& Wise) writes: > |> The speed difference between the two depends on the quality of the > |> optimizations, and context (e.g., in a FOR loop, Pascal's implicit > |> increment can usually be done as part of the test and branch > |> instruction, while in C the increment is an explicit statement, and > |> the optimizer must catch it). > > Since we are in reading compiler output mode, would someone care to compare > the code produced by the following Pascal: > > program test; > var i:integer; > begin > for i:=1 to 10 do {something} > end. > > and C: > > main() > { int i; > for (i=1; i<= 10; i++) /* do something */ > ; > } OK, but I made one change: since MPW C uses 32-bit ints and MPW Pascal integers are 16-bit, I changed the pascal example to use LongInts as they are 32-bits. Here are the two sources: The Pascal Source - UNIT Test; INTERFACE PROCEDURE testit(x:LONGINT); IMPLEMENTATION PROCEDURE SomeThing(a,b: LONGINT); EXTERNAL; PROCEDURE Testit(x:LONGINT); VAR i : LONGINT; BEGIN FOR i := 1 to 10 DO BEGIN SomeThing(i,x); END; END; END. And the MPW C source - void SomeThing(int a, int b); external; void test(int x) { int i; for (i=1;i<= 10;i++) { SomeThing(i,x); } }; And now the results from the MPW DUMPOBJ tool: For Pascal - -- Enter the routine 00000000: 4E56 FFFC 'NV..' LINK A6,#$FFFC 00000004: 2F07 '/.' MOVE.L D7,-(A7) -- set for the FOR loop 00000006: 7E01 '~.' MOVEQ #$01,D7 00000008: 600C '`.' BRA.S *+$000E ; 00000016 -- do the subroutine call 0000000A: 2F07 '/.' MOVE.L D7,-(A7) 0000000C: 2F2E 0008 '/...' MOVE.L $0008(A6),-(A7) 00000010: 4EBA 0000 'N...' JSR SOMETHING ; id: 3 -- process the FOR loop 00000014: 5287 'R.' ADDQ.L #$1,D7 00000016: 700A 'p.' MOVEQ #$0A,D0 00000018: B087 '..' CMP.L D7,D0 0000001A: 6CEE 'l.' BGE.S *-$0010 ; 0000000A -- exit the routine 0000001C: 2E1F '..' MOVE.L (A7)+,D7 0000001E: 4E5E 'N^' UNLK A6 00000020: 2E9F '..' MOVE.L (A7)+,(A7) 00000022: 4E75 'Nu' RTS And the C - -- Enter the routine 00000000: 4E56 0000 'NV..' LINK A6,#$0000 00000004: 48E7 1300 'H...' MOVEM.L D3/D6/D7,-(A7) -- set for the FOR loop 00000008: 2C2E 0008 ',...' MOVE.L $0008(A6),D6 0000000C: 7E01 '~.' MOVEQ #$01,D7 -- do the subroutine call 0000000E: 2F06 '/.' MOVE.L D6,-(A7) 00000010: 2F07 '/.' MOVE.L D7,-(A7) 00000012: 4EBA 0000 'N...' JSR SomeThing ; id: 3 00000016: 508F 'P.' ADDQ.L #$8,A7 -- process the FOR loop 00000018: 2007 ' .' MOVE.L D7,D0 0000001A: 5287 'R.' ADDQ.L #$1,D7 0000001C: 700A 'p.' MOVEQ #$0A,D0 0000001E: B087 '..' CMP.L D7,D0 00000020: 6CEC 'l.' BGE.S *-$0012 ; 0000000E -- exit the routine 00000022: 4CEE 00C8 FFF4 'L.....' MOVEM.L -$000C(A6),D3/D6/D7 00000028: 4E5E 'N^' UNLK A6 0000002A: 4E75 'Nu' RTS Pascal generates a few less instruction. I'm not sure which is faster - I was too lazy to count cycles. But again this shows that they're both pretty close. Declaring the C routines to be Pascal, i.e. Pascal style calling conventions didn't change the byte count. -- michael -- Michael Peirce -- {apple,decwrl}!claris!outpost!peirce -- Peirce Software -- Suite 301, 719 Hibiscus Place -- Macintosh Programming -- San Jose, California 95117 -- & Consulting -- (408) 244-6554, AppleLink: PEIRCE