home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gossip.pyramid.com!olivea!mintaka.lcs.mit.edu!ai-lab!billberry!sundar
- From: sundar@billberry.ai.mit.edu (Sundar Narasimhan)
- Newsgroups: comp.lang.c
- Subject: compilers and code generation (struct vs. array)
- Message-ID: <1ihl0vINNof1@life.ai.mit.edu>
- Date: 7 Jan 93 16:16:31 GMT
- Sender: sundar@billberry (Sundar Narasimhan)
- Reply-To: sundar@ai.mit.edu
- Organization: MIT Artificial Intelligence Laboratory
- Lines: 45
- NNTP-Posting-Host: billberry.ai.mit.edu
-
- 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,
- and transformations -- i.e. multiplication by a 2x2 or 3x3 matrix
- which again can be represented as an array or struct).
-
- Now let's look at one of these operations in detail:
-
- point_add(p1, p2, p3)
- struct point *p1, *p2, *p3;
- {
- p3->x = p1->x + p2->x;
- p3->y = p1->y + p2->y;
- }
-
- or as
- point_add(p1, p2, p3)
- double *p1, *p2, *p3;
- {
- p3[0] = p1[0] + p2[0];
- p3[1] = p1[1] + p2[1];
- }
-
- One can see how to do the other operations, similarly. Obviously for
- speed, these can be made into macros.
-
- Do compilers usually generate the same code in both the above
- cases? (My experience has been that with the optimizers turned
- on Sun CC and GCC do this, but I'd like to know if this is something
- I can rely on -- Without the -O switch, of course, the code generated
- can be wildly different in the various cases).
-
- I've tried benchmarking the two methods under real conditions, however,
- and the evidence has been in-conclusive. (i.e. I've tried it on sparcs
- configured in various ways, and have been surprised to find that on some
- machines one method consistently outperforms the other while neither
- is the best in all configurations and I haven't been able to discern
- why this is the case.) Has anyone else tried this or something
- similar that I could learn from?
-