RE: On the efficiency of FOREACH loops...
Posted in 1999
we are totally in agreement with you, by but from 9 years we come working in 4gl and always found foreach better. > -----Mensaje original----- > De: Jonathan Leffler [SMTP:jleffler@informix.com] > Enviado el: Wednesday, August 11, 1999 7:17 PM > Para: Informix NewsGroup > Asunto: On the efficiency of FOREACH loops... > > > <WHINGE> > * I can live with being called pompous -- on occasion, I am. > * I can live with being told that no-one does benchmarking any more. > * I find it irritating to be told I'm wrong and that benchmarking would > show that I'm wrong without any evidence being provided to demonstrate > that I'm wrong. > It happens; I do get things wrong. But not very often, I think, and > less often when the subject is I4GL or ESQL/C than when the subject is > something else. > </WHINGE> > > FOREACH loops are no less efficient than writing out the same code long > hand, and I think they are demonstrably more efficient. I'm using the > code generated by 7.30.UC1A2 I4GL on Solaris 2.6 to demonstrate my > point, but I don't think that the version or platform used makes a > significant difference to the argument -- the code generation for I4GL > has not changed significantly for a long time, as far as I know. > > Take the following two equivalent pieces of I4GL code: > > -- longhand.4gl -- > > DATABASE Stores > > MAIN > > DEFINE r RECORD LIKE Customer.* > DEFINE i LIKE Customer.Customer_Num > > LET i = 110 > > PREPARE p FROM "SELECT * FROM Customer WHERE Customer_Num < ?" > DECLARE c CURSOR FOR p > > WHENEVER ERROR CONTINUE > > OPEN c USING i > > IF STATUS = 0 THEN > WHILE TRUE > FETCH c INTO r.* > IF STATUS != 0 THEN > EXIT WHILE > END IF > DISPLAY r.* > END WHILE > CLOSE c > END IF > > FREE c > FREE p > > END MAIN > > > -- shorthand.4gl -- > > DATABASE Stores > > MAIN > > DEFINE r RECORD LIKE Customer.* > DEFINE i LIKE Customer.Customer_Num > > LET i = 110 > > PREPARE p FROM "SELECT * FROM Customer WHERE Customer_Num < ?" > DECLARE c CURSOR FOR p > > WHENEVER ERROR CONTINUE > > FOREACH c USING i INTO r.* > DISPLAY r.* > END FOREACH > > FREE c > FREE p > > END MAIN > > > The attachments contain the 4ec files generated from this source code. > There are 121 lines in longhand.4ec and 92 in shorthand.4ec, and the > object files sizes (Sun Sparc 20) are: > > longhand.o: 5573 + 1600 + 0 = 7173 > shorthand.o: 5126 + 1600 + 0 = 6726 > > Looking at the generated code, the major difference is in the while loop > and if statement after the fetch; the actual ESQL/C code is the same > (give or take the cursor name). Because the FOREACH loop code writes > the two if conditions and the while loop in pure C (rather than as a set > of 4GL stack operations), the code should be quicker. > > If you wish to argue that FOREACH is bound to be slower in some older > version of I4GL, please give details, including the 4ec or ec file > generated from the code above. > > <ASIDE> > OK; I have not performed any actual timing tests on the code either. In > fact, I haven't even linked the programs, let alone run them. So, I too > am not doing benchmarking. But I think the difference in the generated > code is sufficient to demonstrate the point I was making. > </ASIDE> > > Yours, > Jonathan Leffler (jleffler@informix.com) #include <witticism.h> > Guardian of DBD::Informix v0.60 (v0.61_02) -- http://www.perl.com/CPAN > Informix IDN for D4GL & Linux -- http://www.informix.com/idn > > PS: In the previous discussions, I also observed that using a FOREACH > loop is less error prone. I suppose it will be treated as an attempt by > my subconscious to prove my point, but the FOREACH code in shorthand.4gl > compiled correctly first time, but I had a syntax error (END WHILE > instead of END IF) in longhand.4gl. QED. <<Archivo: Longhand way of > writing a FOREACH loop; longhand.4ec>> <<Archivo: Shorthand way of > writing a FOREACH loop; shorthand.4ec>>