Simulating polymorphism without the overhead of virtual methods?

Discussion in 'C++' started by jpr, Mar 25, 2006.

  1. jpr

    jpr Guest

    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
     
    jpr, Mar 25, 2006
    #1
    1. Advertising

  2. jpr

    Phlip Guest

    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!!!
     
    Phlip, Mar 25, 2006
    #2
    1. Advertising

  3. jpr

    red floyd Guest

    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!
     
    red floyd, Mar 25, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cppaddict
    Replies:
    12
    Views:
    2,225
    Andy Venikov
    May 26, 2004
  2. Krivenok Dmitry
    Replies:
    13
    Views:
    1,438
    Axter
    Jun 1, 2006
  3. Replies:
    11
    Views:
    696
    James Kanze
    Sep 10, 2006
  4. Belebele
    Replies:
    8
    Views:
    2,290
    naoki
    Dec 13, 2007
  5. feel
    Replies:
    13
    Views:
    666
    James Kanze
    Aug 9, 2008
Loading...

Share This Page