Results 1 to 7 of 7

Thread: which is faster

  1. #1

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    which is faster

    bool func(int* i)
    {
    return (*i == 5);
    }

    bool func(const int& i)
    {
    return (i == 5);
    }
    Last edited by dis1411; Dec 6th, 2006 at 09:20 PM.

  2. #2
    Addicted Member SpS's Avatar
    Join Date
    Jul 2005
    Posts
    201

    Re: which is faster

    Profile and check.
    #Appreciate others by rating good posts !!

    #The Software Peter Principle is in operation when unwise developers "improve" and "generalize" the software until they themselves can no longer understand it, then the project slowly dies.

    #People who are still ignorant of their ignorance are dangerous.

  3. #3
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: which is faster

    I would bet the latter.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: which is faster

    A good compiler should optimise both to the same.

  5. #5

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: which is faster

    i would agree with penagate... however i am providing a simplified situation.. so ignore compiler omptimizations

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: which is faster

    Quote Originally Posted by dis1411
    i would agree with penagate... however i am providing a simplified situation.. so ignore compiler omptimizations
    References, behind the scene, are pointers, just with the dereferencing done for you -- there's no way around it. I would wager that regardless of optimization, the machine code is the same.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: which is faster

    Non-optimizing compilers will generate the same code. But by non-optimizing, I mean the very worst load-for-every-read style compilers.
    For good compilers that however have no information about the content of these two functions, calling the second one could be faster, because the argument is a reference to const. If the compiler keeps the passed value in a callee-saved register in the calling function, it doesn't have to generate code to read it back in from memory, because it can't have changed.
    If the compiler has access to the body of the functions, it should again generate the same code, because it's trivial to prove that the value isn't actually modified. It should also inline the functions if that is enabled.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width