Path: utzoo!dptcdc!jarvis.csri.toronto.edu!mailrus!ames!pasteur!ucbvax!tut.cis.ohio-state.edu!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Messaging overhead Message-ID: <9216@alice.UUCP> Date: 18 Apr 89 19:25:08 GMT References: <1357@stl.stc.co.uk> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 51 In article <1357@stl.stc.co.uk>, dsr@stl.stc.co.uk (David Riches) writes: > Speed is a very important factor when considering > the advantages and disadvantages of routing algorithms, > and as a result I am interested in the overhead due to > messaging in OOP when compared to "conventional" programming. C++ does `messaging' by calling virtual functions. Calling a virtual function in C++ is only a little slower than calling an ordinary member function. How much slower depends, of course, on your machine and C compiler. To get some specific evidence, I tried it on my home machine (a MicroVAX II): struct Foo { void f(); virtual void g(); }; main() { register Foo* fp; fp->f(); fp->g(); } The code generated for fp->f() looks like this: pushl r11 calls $1, and the code for fp->g() looks like this: movl (r11),r0 cvtwl 8(r0),r0 addl3 r0,r11,-(sp) movl (r11),r0 calls $1,*12(r0) So the virtual function call takes three extra instructions and three extra memory references. On this particular machine, a subroutine call/return instruction pair take a lot longer than three memory references, so the extra cost for a virtual function call is insignificant. The overhead would be relatively greater on a machine with extremely fast call/return instructions, but is still small in absolute terms. -- --Andrew Koenig ark@europa.att.com