home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19359 < prev    next >
Encoding:
Text File  |  1993-01-07  |  1.9 KB  |  58 lines

  1. Path: sparky!uunet!gossip.pyramid.com!olivea!mintaka.lcs.mit.edu!ai-lab!billberry!sundar
  2. From: sundar@billberry.ai.mit.edu (Sundar Narasimhan)
  3. Newsgroups: comp.lang.c
  4. Subject: compilers and code generation (struct vs. array)
  5. Message-ID: <1ihl0vINNof1@life.ai.mit.edu>
  6. Date: 7 Jan 93 16:16:31 GMT
  7. Sender: sundar@billberry (Sundar Narasimhan)
  8. Reply-To: sundar@ai.mit.edu
  9. Organization: MIT Artificial Intelligence Laboratory
  10. Lines: 45
  11. NNTP-Posting-Host: billberry.ai.mit.edu
  12.  
  13. Let's say a point is defined as
  14.     struct point { 
  15.         double x, y;
  16.         }
  17. or as
  18. typedef double point[2];
  19.  
  20. I'm interested in the usual operation on points. (i.e. 
  21. additions, subtractions, multiplications, co-ordinate access,
  22. and transformations -- i.e. multiplication by a 2x2 or 3x3 matrix 
  23. which again can be represented as an array or struct).
  24.  
  25. Now let's look at one of these operations in detail:
  26.  
  27. point_add(p1, p2, p3)
  28. struct point *p1, *p2, *p3;
  29. {
  30.     p3->x = p1->x + p2->x;    
  31.     p3->y = p1->y + p2->y;    
  32. }
  33.  
  34. or as
  35. point_add(p1, p2, p3)
  36. double *p1, *p2, *p3;
  37. {
  38.     p3[0] = p1[0] + p2[0];
  39.     p3[1] = p1[1] + p2[1];
  40. }
  41.  
  42. One can see how to do the other operations, similarly. Obviously for
  43. speed, these can be made into macros.
  44.  
  45. Do compilers usually generate the same code in both the above
  46. cases? (My experience has been that with the optimizers turned
  47. on Sun CC and GCC do this, but I'd like to know if this is something
  48. I can rely on -- Without the -O switch, of course, the code generated
  49. can be wildly different in the various cases).
  50.  
  51. I've tried benchmarking the two methods under real conditions, however,
  52. and the evidence has been in-conclusive. (i.e. I've tried it on sparcs
  53. configured in various ways, and have been surprised to find that on some
  54. machines one method consistently outperforms the other while neither
  55. is the best in all configurations and I haven't been able to discern
  56. why this is the case.) Has anyone else tried this or something
  57. similar that I could learn from?
  58.