home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!asuvax!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
- From: dave@cs.arizona.edu (Dave Schaumann)
- Newsgroups: comp.lang.c
- Subject: Re: compilers and code generation (struct vs. array)
- Message-ID: <1993Jan8.001225.16732@organpipe.uug.arizona.edu>
- Date: 8 Jan 93 00:12:25 GMT
- References: <1ihl0vINNof1@life.ai.mit.edu>
- Sender: news@organpipe.uug.arizona.edu
- Reply-To: dave@cs.arizona.edu (Dave Schaumann)
- Organization: University of Arizona
- Lines: 43
- In-Reply-To: sundar@billberry.ai.mit.edu (Sundar Narasimhan)
-
- In article <1ihl0vINNof1@life.ai.mit.edu>, sundar@billberry (Sundar Narasimhan) writes:
- >Let's say a point is defined as
- > struct point {
- > double x, y;
- > }
- >or as
- >typedef double point[2];
- >
- >I'm interested in the usual operation on points. (i.e.
- >additions, subtractions, multiplications, co-ordinate access,
- [...]
- >Do compilers usually generate the same code in both the above
- >cases?
-
- It's dangerous to say anything about what "compilers usually generate";
- there's a large range of qualities of implementation out there.
-
- That said, the short answer is "probably yes".
-
- The long answer is that from a code generation point of view, a struct
- reference can be viewed as a base address plus a constant offset, and
- an array reference can be viewed as a base address plus a (potentially)
- variable offset. In the case where you always use constant indexes in
- your arrays, it's a fairly easy optimization to generate the same code
- as would be generated for a struct offset. Since C compilers are required
- to recognize constant expressions anyway, one would assume that most
- compilers would get this.
-
- On the other hand, if it doesn't get this right, it's probably generating
- poor code elsewhere too, so that the difference between p.x and p[0] is
- probably going to be overwhelmed by other things anyway.
-
- I'd say just go with what seems most natural. This is probably good
- advice in any coding concern. The big wins in optimization are far
- more in the area of designing basic algorithms than they are in the
- fine-tuning area of deciding between "p.x" and "p[0]". If your program
- runs too slow, use a profiler. Humans can do a pretty fair job when
- it comes to designing a complex system like a computer program, but
- when it comes to finding where it spends most of its processing time,
- we come in a (very) distant second to a simple automated accounting.
-
- --
- Caught an internal error--.brainrc restored
-