Thanks for your extremely quick response and suggestions. I've reworked the code so it no longer does as much IO. The difference is striking! http://linuxjunk.blogspot.com/2009/11/are-templates-faster-than-subclasses.html 5.9s for derived class, 0.008s for the templated version. I'm guessing that it's automatically inlining the code in the template version? And the virtual method can not be inlined (is this ever possible?). Nav On Tue, Nov 24, 2009 at 11:55:22AM -0800, Brian Budge wrote: > Your program is certain to be IO bound. This is not a good test of > the cost of any of your computations (unless that cost is IO). > > If you have a program which can use inlined template functions instead > of virtual function calls, and the function is "called" very > frequently, the inlined template functions should be much faster (this > is more due to saving of registers, etc, rather than the virtual > function lookup). This is a rule of thumb, not a guarantee. > > My hunch is that you need to construct a better benchmark (hint, make > sure your call overhead is the bottleneck, and not IO (cout), and make > sure that all of your results are actually used, otherwise they may be > optimized out). > > Brian > > On Tue, Nov 24, 2009 at 11:44 AM, Nava Whiteford <new@xxxxxxxxxxxxx> wrote: > > Hi, > > > > Are template classes faster than using a virtual derived class via a > > pointer to its base class? > > > > Intuition tells me it should be faster, and reasonably significantly so. > > However I put together the following test: > > > > http://linuxjunk.blogspot.com/2009/11/are-templates-faster-than-subclasses.html > > > > Which suggests to me that there isn't really a lot in it. > > > > I'd welcome any comments on this. > > > > Nav > > -- Nav Work: 01865 854873 Mob : 07518-358405