Path: utzoo!mnetor!tmsoft!torsqnt!jarvis.csri.toronto.edu!rutgers!dptg!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: Efficiency of C++ relative to C?? Summary: The ``expensive'' features of C++ provide functionality not available in C. You don't pay for them if you don't use them. Message-ID: <9924@alice.UUCP> Date: 19 Sep 89 15:05:09 GMT References: <16493@watdragon.waterloo.edu> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 50 In article <16493@watdragon.waterloo.edu>, dvadura@watdragon.waterloo.edu (Dennis Vadura) writes: > I have the following question: > > Has anyone performed a study comparing the execution time cost > of C++ language features compared to C? In particular I am > interested in the following problem. Given a particular program, > code it in C and code it in C++ using the full power of C++. > Time the execution speed of both programs on the same input > data. > > Then ask how much of the cost of the C++ program is due to the > use of virtual functions, and what is the call overhead for > a virtual function when compared to the call overhead for a > normal call in C? > Yes, virtual functions are more expensive than ordinary functions (about 4 or 5 more memory references per call), but you don't have to use virtual functions if they aren't needed. Typically a virtual function call in C++ replaces a switch statement in C. If the cases of the switch each call functions, then the C++ version will certainly win. If the cases each execute a single line of code then it will depend the efficiency of the code generated by the compiler for the switch. In your question you assume ``the full power of C++.'' Clearly if you use every feature wherever it could fit (for example, make every function and base class virtual), you could take a performance hit, but if you use the power of the language appropriately, I think you will come out ahead. Note that it will take some time to learn the performance implications of the various C++ features, and to develop good judgement about when to use which. Also, when you're thinking about getting the last iota of performance, remember that the C++ translator generates C, so you could always have written the same program in C, and any compiler generates machine code, so you could always have written the same program in assembler. That is to say, if you're considering higher level languages at all, performance is not your only criterion, you're also concerned about programmer productivity. So, if you're using C now and you're unhappy with it because even though you are a very good C programmer, your programs don't run fast enough, C++ is unlikely to help you much (or at all). Try assembly language or a better optimizer. If you're unhappy with C because programming large applications is too expensive, then try C++. You will be able to build more complicated applications than you could before, and, if you program carefully, you will not incur any performance penalty. -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229