Simulating polymorphism without the overhead of virtual methods?

J

jpr

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
 
P

Phlip

jpr said:
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.
 
R

red floyd

Phlip said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top