Performance using member of

Discussion in 'C++' started by Lilith, Feb 11, 2006.

  1. Lilith

    Lilith Guest

    If I have a class object and a pointer to the object is there a
    performance hit for invoking a member function using member of (.) vs
    member of (->) due to pointer arithmetic? From an amateur point of
    view I'd initially think that the pointer arithmetic would cause a
    slow down. On the other hand pointer arithmetic shouldn't even be an
    issue since the member functions aren't actually part of the object
    but instead a member of the class.

    Please advise.

    --
    Thanks,
    Lilith
     
    Lilith, Feb 11, 2006
    #1
    1. Advertising

  2. Lilith

    TB Guest

    Lilith sade:
    > If I have a class object and a pointer to the object is there a
    > performance hit for invoking a member function using member of (.) vs
    > member of (->) due to pointer arithmetic? From an amateur point of
    > view I'd initially think that the pointer arithmetic would cause a
    > slow down. On the other hand pointer arithmetic shouldn't even be an
    > issue since the member functions aren't actually part of the object
    > but instead a member of the class.
    >
    > Please advise.
    >


    Slow down? Do you expect a noticable performance decay?
    No difference of any vital concern. Don't bother
    reflecting upon it.

    --
    TB @ SWEDEN
     
    TB, Feb 11, 2006
    #2
    1. Advertising

  3. Lilith

    Mike Wahler Guest

    "Lilith" <> wrote in message
    news:...
    > If I have a class object and a pointer to the object is there a
    > performance hit for invoking a member function using member of (.) vs
    > member of (->) due to pointer arithmetic? From an amateur point of
    > view I'd initially think that the pointer arithmetic would cause a
    > slow down. On the other hand pointer arithmetic shouldn't even be an
    > issue since the member functions aren't actually part of the object
    > but instead a member of the class.


    Write your code so as to be as simple and easily
    readable as possible. Only worry about performance
    if you can *prove* it's an issue (by measurement).

    I strongly doubt you'll see a measurable difference
    between using '.' operator vs '->' operator.

    -Mike
     
    Mike Wahler, Feb 11, 2006
    #3
  4. On Sat, 11 Feb 2006 09:41:31 -0600, Lilith <> wrote:

    >If I have a class object and a pointer to the object is there a
    >performance hit for invoking a member function using member of (.) vs
    >member of (->) due to pointer arithmetic? From an amateur point of
    >view I'd initially think that the pointer arithmetic would cause a
    >slow down. On the other hand pointer arithmetic shouldn't even be an
    >issue since the member functions aren't actually part of the object
    >but instead a member of the class.
    >
    >Please advise.


    You are correct when you say that pointer arithmetic shouldn't be an
    issue. The physical address of a particular non-static member function
    should be the same whether one calls it on an object or on a pointer.
    It should be actually the same for all objects of the same class ...
    if anything, calling through a pointer (i.e. with -> syntax) is
    conceivably faster than with the dot operator because each member
    function gets passed a hidden argument: the "this" pointer of the
    object involved. Since the pointer already contains that address, it
    shouldn't have to be "looked up" or calculated with some kind of
    pointer arithmetic.

    But if you think about how the machine code generated by the compiler
    would have to be seen by the CPU, ask yourself: What is an "object"?
    According to most definitions of an object, it is an area of storage
    with a certain structure as determined by the class declaration. But
    any area of storage at that low level would have to be accessed
    through memory addresses, IOW pointers. The only difference is that a
    pointer occupies storage of its own, but every compiler is "smart
    enough" to be able to optimize away that one level of indirection
    under most circumstances.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Feb 12, 2006
    #4
    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. jm
    Replies:
    1
    Views:
    516
    alien2_51
    Dec 12, 2003
  2. Replies:
    1
    Views:
    310
    Martin Dechev
    Oct 7, 2004
  3. 0to60
    Replies:
    4
    Views:
    520
    jeffc
    Nov 21, 2003
  4. Replies:
    3
    Views:
    480
  5. Software Engineer
    Replies:
    0
    Views:
    338
    Software Engineer
    Jun 10, 2011
Loading...

Share This Page