Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!bu-cs!encore!multimax From: pierson@multimax (Dan Pierson) Newsgroups: comp.lang.c++ Subject: Re: Efficiency of C++ relative to C?? Message-ID: <9962@multimax.Encore.COM> Date: 19 Sep 89 15:04:46 GMT References: <16493@watdragon.waterloo.edu> Sender: news@Encore.COM Reply-To: pierson@multimax (Dan Pierson) Organization: Encore Computer Corp Lines: 43 In article <16493@watdragon.waterloo.edu>, dvadura@watdragon (Dennis Vadura) writes: > 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? To paraphrase one of Danny Bobrow's comments when people raise the analogous question about the Common Lisp Object System: It isn't realistic to compare the cost of a virtual mentod with the cost of a normal function call; if all you wanted was a normal function call you would use one. A relevant question is: what is cost of a virtual function call compared to the cost of the C code you would use to do the same thing the virtual function call is trying to do? Such code might look like: void print (object *thing) { switch (thing->type) { case OBJ_PICTURE: print_picture(thing); break; ... } } In this case, a virtual function call should be compared to: C call + structure reference via pointer + switch statement I suspect that you will find that C++ virtual methods compare rather well to the above. Caveat: Of course it is possible to write C++ code that uses virtual methods when normal methods would do the job. It's also possible to write C++ code that uses normal methods when inline methods would be more appropriate. It's possible to write poor code in C too, but that shouldn't be the point of a language comparison. -- dan In real life: Dan Pierson, Encore Computer Corporation, Research UUCP: {talcott,linus,necis,decvax,ihnp4}!encore!pierson Internet: pierson@multimax.arpa