🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #6711

Re: Common subexpression optimization

#6711
From: David T. Sandberg
Date: Mon, 12 Feb 1990 23:35
42 lines
2000 bytes
In article <767@enea.se> sommar@enea.se (Erland Sommarskog) writes:
:Kristoffer Eriksson (ske@pkmab.se) writes:
: >Jim Giles (jlg@lambda.UUCP) writes:
: >>  int a[200][2]; int *p; int *q;
: >>  ...
: >>  p = &a; q = p+1;
: >>  for (i=0;i<200;i++){
: >>    *p = expr1(i);
: >>    *q = expr2(i);
: >>    p += 2; q += 2;
: >>  };
: >
: >would be much more "natural" and less obscure this way:
: >
: >  int a[200][2]; int *p;            
: >  p = &a[0][0];			/* Start fromthe first int of a. */
: >  for (i = 0; i < 200; i++) {
: >      *(p++) = expr1(i);
: >      *(p++) = expr2(i);
: >  };
:
:OK, I don't speak C, maybe this is a standard approach once you
:know the language, but I find Kristoffer's proposal to be at
:least as obscure as Jim Giles' right-hand example.

You're right: you don't speak C.  Kristoffer's version is a very
common way to do operations like this in C.  I've written several
bits of code nearly identical to that in the last week.  And as a
C programmer, I found Kristoffer's version instantly comprehendable,
whereas Jim Giles' right hand example required a second look.  And
I daresay that Kristoffer's version would compile into more efficient
code.

BTW, you don't even need the parens in the lines which do the
assignment and increment to pointer *p, because it evaluates that
way anyway, so I would have written it as:

      *p++ = expr1( i );

-- 
                "Now beat it, or the red guy screams again!"
David Sandberg, dts@quad.sialis.mn.org or ..uunet!rosevax!sialis!quad!dts
Technical Details
Message-ID: <449@quad.uucp>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!rutgers!umn-d-ub!cs.umn.edu!nis!quad!dts
References: <14229@lambda.UUCP> <2844@pkmab.se > <767@enea.se>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!rutgers!umn-d-ub!cs.umn.edu!nis!quad!dts
From: dts@quad.uucp (David T. Sandberg)
Newsgroups: comp.lang.misc
Subject: Re: Common subexpression optimization
Message-ID: <449@quad.uucp>
Date: 12 Feb 90 23:35:50 GMT
References: <14229@lambda.UUCP> <2844@pkmab.se > <767@enea.se>
Reply-To: dts@quad.uucp (David T. Sandberg)
Organization: Quadric Systems, Richfield MN
Lines: 42