Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simulating polymorphism without the overhead of virtual methods?

Reply
Thread Tools

Simulating polymorphism without the overhead of virtual methods?

 
 
jpr
Guest
Posts: n/a
 
      03-25-2006
hello ...

I'm translating a compressed graph class (for large graphs) from Java
to C++. Currently I'm writing a function that should return an iterator
over the successors of a given node. These successors are represented
by int's.

Depending on the way the graph is compressed, I might have to extract
the successors from the bitstream holding the graph in several ways.
Because of this, the Java version had four different iterator classes,
all extending a common base class; it would construct and return the
appropriate one.

I'd like to avoid having to declare my iterator members virtual because
of the overhead of a virtual function call (and the missed chance to
inline). However, I want to be able to return a single type to hide any
detail of how I come up with these types from the client.

Is there a trick to do this sort of thing, or am I chasing a hopeless
dream?

thanks!
-jacob

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      03-25-2006
jpr wrote:

> Is there a trick to do this sort of thing, or am I chasing a hopeless
> dream?


Get your C++ code working, with unit tests. Then profile it to see where the
real bottlenecks are; not the ones you guess will be. They might surprise
you. And there also might not be any.

Use the unit tests to time things, and to preserve behavior while you adjust
the bottlenecks and tune things.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      03-25-2006
Phlip wrote:
> jpr wrote:
>
>> Is there a trick to do this sort of thing, or am I chasing a hopeless
>> dream?

>
> Get your C++ code working, with unit tests. Then profile it to see where the
> real bottlenecks are; not the ones you guess will be. They might surprise
> you. And there also might not be any.
>
> Use the unit tests to time things, and to preserve behavior while you adjust
> the bottlenecks and tune things.
>


Thank you, thank you, THANK YOU!!!!!!!!!!

I get so sick of seeing posts here where people are so overly concerned
about "the most efficient" way to do something, without having
benchmarked it. They seem to forget Hoare's/Knuth's law: "Premature
optimization is the root of all evil."

Your answer was the best response to this that I have seen. Mine tend
to be snarky, alas.

Thank you again!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Polymorphism without virtual in C++ feel C++ 13 08-09-2008 10:28 AM
Polymorphism (without virtual functions) with a method template. Belebele C++ 8 12-13-2007 03:02 AM
same overhead in calling virtual and non virtual member function...? ypjofficial@indiatimes.com C++ 11 09-10-2006 06:08 PM
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
virtual base class overhead Thomas Maier-Komor C++ 0 10-20-2004 01:34 PM



Advertisments