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

  1. Xref: sparky comp.lang.c:19564 comp.lang.c++:18982
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!att!att!allegra!alice!bs
  3. From: bs@alice.att.com (Bjarne Stroustrup)
  4. Newsgroups: comp.lang.c,comp.lang.c++
  5. Subject: Re: C/C++ Speed
  6. Message-ID: <24592@alice.att.com>
  7. Date: 12 Jan 93 02:56:20 GMT
  8. Article-I.D.: alice.24592
  9. References: <1ipsk5INNf5m@aludra.usc.edu> <dak.726696239@tabaqui> <1993Jan11.163852.19740@informix.com>
  10. Distribution: usa
  11. Organization: AT&T Bell Laboratories, Murray Hill NJ
  12. Lines: 163
  13.  
  14.  
  15.  
  16. In answer to some messages
  17. cshaver@informix.com (Craig Shaver @ Informix Software, Inc.) writes
  18.  
  19.  > >>    I also heard the size of C++ program is generally bigger than C.
  20.  > >>I am a C programmer trying to learn C++.  So, don't blame me if I have 
  21.  > >>created any misconception about C++.
  22.  > >
  23.  > >For one minor detail: including printf/scanf can include more code than
  24.  > >is actually used. An intelligent C++ linker will only get that parts
  25.  > >of the stream library REALLY needed.
  26.  > >
  27.  > 
  28.  > Who implements an intelligent C++ linker???  I was under the impression that 
  29.  > you get all the functionality in a class when you link, whether you use it 
  30.  > or not.  
  31.  
  32. Let me try to clear up one or two points.
  33.  
  34. Consider first a somewhat minimal C and C++ program x1.c:
  35.  
  36.     #include<stdio.h>
  37.  
  38.     main()
  39.     {
  40.         int i;
  41.         for (i = 0; i<1000000; i++) printf("Hi,mom!\n");
  42.     }
  43.  
  44. and its more C++ looking cousin x2.c:
  45.  
  46.     #include<iostream.h>
  47.  
  48.     main()
  49.     {
  50.         int i;
  51.         for (i = 0; i<1000000; i++) cout << "Hi,mom!\n";
  52.     }
  53.  
  54.  
  55. I compiled and ran x1.c and x2.c:
  56.  
  57. c: cc x1.c
  58. c: size a.out
  59. text    data    bss    dec    hex
  60. 12288    6144    7608    26040    65b8
  61. c: time a.out > /dev/null
  62. 25.5u 0.5s 37r      a.out
  63.  
  64. c: PTCC x1.c
  65. c: size a.out
  66. text    data    bss    dec    hex
  67. 12288    6144    7620    26052    65c4
  68. c:  time a.out > /dev/null
  69. 25.7u 0.4s 33r      a.out
  70.  
  71. PTCC is the driver for my standard off the-shelf Cfront 3.0 (i.e. I'm
  72. not using any technology you couldn't buy half a year ago). Note that
  73. the size of the generated code is essentially the same. So is the speed.
  74. Running these examples a few times to eliminate random error in the timing
  75. mechanism shows that the run-time isn't biased one way or the other.
  76.  
  77. This is what you should expect for a program in the common sub-set
  78. of C and C++. There is fundamental reasons for that. You should expect
  79. identical code from two C and C++ compilers using the same technology.
  80. The only possibly SYSTEMATIC difference I can think of is that a C++
  81. compiler can use better function call sequences than a C compiler
  82. that doesn't apply global optimization because in many cases a C
  83. compiler must guard against possible calls with differing numbers
  84. of arguments where a C++ compiler doesn't need to because of C++'s
  85. stronger type checking. In most C and C++ compilers, this difference
  86. is theoretical, but I'm told that in Zortech C++ it is real (i.e. C++
  87. programs are ever so slightly faster than their C equivalents).
  88. However, this is all noise, I doubt the difference between C and C++
  89. in this kind of comparison matters to any real programmers.
  90. The difference is far smaller than differences between different
  91. C compilers - but surprisingly, it is in C++'s favor.
  92.  
  93.     Programs in the common subset of C and C++ results in
  94.     equal sized code that execute at equal speed.
  95.  
  96. If that conclusion doesn't appear to hold, check if your C and C++
  97. compilers are of similar quality. If your C++ compiler appears to
  98. loose badly you have the option of using a Cfront variant to get the
  99. benefits of your C compiler's code generation facilities. If your
  100. C compiler loose badly, switch to C++ even if you aren't ready to
  101. use the ``++ features.''
  102.  
  103. Now, a common argument is ``OK, so C++ can match C for a C programs
  104. but as soon as you use the REAL C++ features your programs get
  105. bigger and slower.'' Clearly you can write big and slow programs
  106. in any language (even C), but you don't necessarily take a performance
  107. hit when you start using C++. Consider x2.c. It uses the C++ stream
  108. I/O library that is certainly bigger than C's stdio and is unlikely
  109. to be tuned to the same degree as stdio. It is also a library that
  110. uses a very large sub-set of C++'s features in its interface and
  111. implementation (operator overloading, multiple inheritance, virtual
  112. functions, etc.):
  113.  
  114. c: PTCC x2.c
  115. c: size a.out
  116. c: text    data    bss    dec    hex
  117. 17408    2048    0    19456    4c00
  118. c: time a.out > /dev/null
  119. 32.8u 1.0s 43r      a.out
  120.  
  121. Surprisingly enough, the code generated for x2.c is noticeably smaller
  122. than the code generated for x1.c (75% of x1.o) though - as expected it
  123. runs a bit slower (29% user cpu time, 16% better elapse time).
  124.  
  125. I claim, but cannot prove, that the run-time overhead is primarily a
  126. difference in tuning. Other programs that rely heavily on C++ features
  127. show improvements over their C counterparts - and others again show
  128. overhead. The differences does not appear systematic to me; that is,
  129. they are differences in design and effort, rather than inherent overhead
  130. in C or C++.
  131.  
  132. The space advantage of the C++ program is an advantage of the same kind;
  133. that is, it is there because a little extra care and thought was spent.
  134. Other implementations of stream I/O will show different space and time
  135. usage, as will different implementations of stdio. To do simple things
  136. only the essential parts of the stream I/O library is brought in. You
  137. don't actually need a very ``intelligent'' linker, the dumb old Unix
  138. ld will do: Just manually split your implementation into several .c
  139. files. A simple example:
  140.  
  141. X.h:
  142.     class X {
  143.         // details
  144.     public:
  145.         void f();    // common function
  146.         void g();    // uncommon function
  147.         // more functions
  148.     };
  149.  
  150.     
  151. X1.c:
  152.     // common functions:
  153.  
  154.     void X::f() { ... }
  155.  
  156.  
  157. X2.c:
  158.     // uncommon functions:
  159.  
  160.     void X::g() { ... }
  161.  
  162. Now, any half-way decent archive program can bring in the object code
  163. for X1.c (only) for programs that use the common functions (only) and
  164. leave the expense of bringing in the object code for X2.c for the
  165. programs that actually use functions defined in X2.c.
  166.  
  167. There exist linkers that can do that without human help (mostly in
  168. the PC world), I just happen not to have one. I think it is important
  169. to note that this technique and the tools that supports it carried
  170. over from C to C++. We wasn't at the mercy of some ``smart'' and
  171. possibly espensive or unavailable technology. We don't have to forget
  172. or loose all of our effective techniques in moving from C to C++.
  173. We should - as ever - use them with a suitable amount of judgement.
  174.  
  175. C++ was designed not to leave room ``below'' for a lower level
  176. language, except assembler for machine specific operations.
  177.