Thread: Function calling - which is better, and why? ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    20

    Post Function calling - which is better, and why? ...

    OK ... which of these two is better, and why?

    Example 1

    Code:
    void CHighResolutionTimer::SecondsElapsed(double *ElapsedTime)
    {
    	QueryPerformanceCounter((LARGE_INTEGER*) &m_i64_ElapsedTime);
    	*ElapsedTime = double(m_i64_ElapsedTime - m_i64_StartTime) / double(m_i64_Frequency);
    }
    Example 2

    Code:
    double CHighResolutionTimer::SecondsElapsed(void)
    {
    	QueryPerformanceCounter((LARGE_INTEGER*) &m_i64_ElapsedTime);
    	return double(m_i64_ElapsedTime - m_i64_StartTime) / double(m_i64_Frequency);
    }

    I could then call then like ...


    CBlah.SecondsElapsed(&var);
    or
    var = CBlah.SecondsElapsed();


    I'm interested in the performance rather than the readability.

    Regards
    Last edited by Shag; 07-01-2002 at 06:33 PM.

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    In example 2 a temporary variable is created to return, while in example 1 there is no temporary variable created. Example 1 should therefore have a slight performance benefit.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    But you don't have the syntactic benefit of being able to nest calls.

    double movement = velocity * timer.getSecondsElapsed();

    is impossible with the pass a pointer and fill it notation.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I'm pretty sure that 2 should run faster. 2 has to do an extra copy, but 1 has to access memory through a pointer, which can be relatively expensive.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I did a little test (I used sqrt() versus QueryPerformanceCounter()), and Example 2 was indeed just a tad bit faster.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Just thought about it a little more... #2 should definately be faster with optimizations since the single double could probably be handled at register level while pass through reference... well, that's memory no matter how you cut it.
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    20
    I finally got a machine to acturately test the two returning methods:

    VC++6 ... No optomisation, no inlining

    Worst case - the two were identical
    Best case - version two about 60% faster.

    Thanks for all the input

    Cheers all

  8. #8
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    The Second is faster
    because:

    the varriable is returned in the Proccessor register eax

    while the first uses the stack memory with push and pop
    therefore is slower...

Popular pages Recent additions subscribe to a feed