passing class to a function in c++ - performance question

Discussion in 'C++' started by Profil1, Mar 30, 2005.

  1. Profil1

    Profil1 Guest

    Hi,
    I'm writing a code that has to be as efficient as possible both in terms of
    memory use and execution speed. I'll have to pass a class instance (which is
    an 'intelligent' array) to a function (multiple times). Taking this into
    consideration, should I pass it as a pointer or reference? Is there any book
    or site that teaches stuff like this (optimizing code for performance)?
    thanks for any help
    Profil1, Mar 30, 2005
    #1
    1. Advertising

  2. Profil1

    BigBrian Guest

    How references are implemented are up to the compiler writer, however
    they're usually implemented as pointers. You could implement your code
    both ways and test the runtime performance, or look at the object code
    to see what it's doing. However, the rule of thumb is use references
    unless you *have* to use pointers. In this case, there is no
    requirement specifically for pointers, so use a reference. But if
    you're really concerned with performance to the extent of worrying
    about the difference between passing a reference or a pointer, maybe
    you should write your application in assembler instead of C++. Didn't
    somebody famus say, "premature optimization is evil", or something like
    that?
    BigBrian, Mar 30, 2005
    #2
    1. Advertising

  3. Profil1 wrote:
    >
    > Hi,
    > I'm writing a code that has to be as efficient as possible both in terms of
    > memory use and execution speed. I'll have to pass a class instance (which is
    > an 'intelligent' array) to a function (multiple times). Taking this into
    > consideration, should I pass it as a pointer or reference? Is there any book
    > or site that teaches stuff like this (optimizing code for performance)?
    > thanks for any help


    There is only one way to figure out: Time it.

    But usually both methods (pass by pointer, pass per reference)
    have equal performance. This is so, because the compiler implements
    'pass per reference' by passing a pointer under the hood. So in
    the end it turns out, that you do the very same thing with different
    syntax.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Mar 30, 2005
    #3
  4. Profil1 wrote:
    > Hi,
    > I'm writing a code that has to be as efficient as possible both in terms of
    > memory use and execution speed. I'll have to pass a class instance (which is
    > an 'intelligent' array) to a function (multiple times). Taking this into
    > consideration, should I pass it as a pointer or reference? Is there any book
    > or site that teaches stuff like this (optimizing code for performance)?
    > thanks for any help


    In practice, usually, passing by pointer vs by reference has the same
    performance. However, since a reference cannot be changed once it is
    seated, in theory, the optimizer may be more agressive with code
    optimizations with references vs optimizations with pointers. So,
    passing by reference may yield better optimized code (in theory).

    The only real way to find out is to try it.

    This also applies to the "register" keyword. The register keyword was
    usually a hint to the compiler to allocate a register. An optimizer can
    now read that it can't be aliased and hence it can perform better
    optimizations. Again, this is theory only, what your compiler does is
    the only thing that matters.
    Gianni Mariani, Mar 30, 2005
    #4
  5. Profil1

    lilburne Guest

    Profil1 wrote:

    > Hi,
    > I'm writing a code that has to be as efficient as possible both in terms of
    > memory use and execution speed. I'll have to pass a class instance (which is
    > an 'intelligent' array) to a function (multiple times). Taking this into
    > consideration, should I pass it as a pointer or reference? Is there any book
    > or site that teaches stuff like this (optimizing code for performance)?
    > thanks for any help
    >
    >


    If you want to improve performance first profile your code to determine
    where the time is spent. If as a result the function which is being
    called is really shown to be the bottleneck contrive to call the
    function fewer times. I.e., find an algorithm that is O(log n) rather
    than O(n^2). Then look to improving the way that the function does its
    work for example remove sqrt(), etc.
    lilburne, Mar 30, 2005
    #5
  6. lilburne wrote:
    >
    >
    > Profil1 wrote:
    >
    >> Hi,
    >> I'm writing a code that has to be as efficient as possible both in
    >> terms of memory use and execution speed. I'll have to pass a class
    >> instance (which is an 'intelligent' array) to a function (multiple
    >> times). Taking this into consideration, should I pass it as a pointer
    >> or reference? Is there any book or site that teaches stuff like this
    >> (optimizing code for performance)?
    >> thanks for any help
    >>

    >
    > If you want to improve performance first profile your code to determine
    > where the time is spent. If as a result the function which is being
    > called is really shown to be the bottleneck contrive to call the
    > function fewer times. I.e., find an algorithm that is O(log n) rather
    > than O(n^2). Then look to improving the way that the function does its
    > work for example remove sqrt(), etc.
    >
    >


    Seconded.

    Trying to optimize code at this level will be an exercise in wasting
    time. Premature Optimisation as BigBrian said.

    The real bottle neck will be elsewhere, not whether a ref or pointer is
    passed.
    Andrew McDonagh, Mar 30, 2005
    #6
  7. Profil1

    lilburne Guest

    Andrew McDonagh wrote:

    > lilburne wrote:
    >
    >>
    >> If you want to improve performance first profile your code to
    >> determine where the time is spent. If as a result the function which
    >> is being called is really shown to be the bottleneck contrive to call
    >> the function fewer times. I.e., find an algorithm that is O(log n)
    >> rather than O(n^2). Then look to improving the way that the function
    >> does its work for example remove sqrt(), etc.
    >>
    >>

    >
    > Seconded.
    >
    > Trying to optimize code at this level will be an exercise in wasting
    > time. Premature Optimisation as BigBrian said.
    >
    > The real bottle neck will be elsewhere, not whether a ref or pointer is
    > passed.
    >


    Sometimes micro optimization can be successful, but you need
    a tool, like quantify, to point it out. Guessing and
    supposing is nearly always wrong.
    lilburne, Mar 30, 2005
    #7
  8. lilburne wrote:
    > Andrew McDonagh wrote:
    >
    >> lilburne wrote:
    >>
    >>>
    >>> If you want to improve performance first profile your code to
    >>> determine where the time is spent. If as a result the function which
    >>> is being called is really shown to be the bottleneck contrive to call
    >>> the function fewer times. I.e., find an algorithm that is O(log n)
    >>> rather than O(n^2). Then look to improving the way that the function
    >>> does its work for example remove sqrt(), etc.
    >>>
    >>>

    >>
    >> Seconded.
    >>
    >> Trying to optimize code at this level will be an exercise in wasting
    >> time. Premature Optimisation as BigBrian said.
    >>
    >> The real bottle neck will be elsewhere, not whether a ref or pointer
    >> is passed.
    >>

    >
    > Sometimes micro optimization can be successful, but you need a tool,
    > like quantify, to point it out. Guessing and supposing is nearly always
    > wrong.
    >
    >


    agreed, like you say, so long as we have proof from some a tool like
    Rational's Quantify.

    The problems start when a developer 'knows' that doing it 'this way' is
    faster than that way, even if it means the code is less clear and
    usually tightly coupled, when in fact the area of concern is called once
    in a blue moon.
    Andrew McDonagh, Mar 30, 2005
    #8
  9. Profil1

    aleko Guest

    If you're going to do any performance testing, don't forget to do them
    on a Release build. :) Optimizing debug code is not recommended as it
    may defeat the compiler's optimizer, not to mention that the end
    (Release) code will probably be different from what you expected,
    anyway.

    Also, before you try to fix it, find out exactly where it's broken. Use
    a profiler to identify hotspots. You'll get much better results this
    way.

    Hope this helps,

    Aleko


    Profil1 wrote:
    > Hi,
    > I'm writing a code that has to be as efficient as possible both in

    terms of
    > memory use and execution speed. I'll have to pass a class instance

    (which is
    > an 'intelligent' array) to a function (multiple times). Taking this

    into
    > consideration, should I pass it as a pointer or reference? Is there

    any book
    > or site that teaches stuff like this (optimizing code for

    performance)?
    > thanks for any help
    aleko, Mar 30, 2005
    #9
  10. Profil1

    Andre Caldas Guest

    > should I pass it as a pointer or reference? Is there any book
    > or site that teaches stuff like this (optimizing code for performance)?


    If you don't accept any NULLs, then pass it as reference.
    Andre Caldas, Apr 7, 2005
    #10
    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:
    507
    alien2_51
    Dec 12, 2003
  2. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  3. 0to60
    Replies:
    4
    Views:
    511
    jeffc
    Nov 21, 2003
  4. James Vanns
    Replies:
    7
    Views:
    7,030
    Evan Carew
    Jan 21, 2004
  5. Software Engineer
    Replies:
    0
    Views:
    323
    Software Engineer
    Jun 10, 2011
Loading...

Share This Page