Click here to Skip to main content
14,663,993 members
Articles » Languages » C / C++ Language » Beginners
Tip/Trick
Posted 20 Sep 2020

Tagged as

Stats

2.6K views
40 downloads
1 bookmarked

C++: Faster to Retrieve Data By Reference Parameter or Returning a Reference?

Rate this:
2.33 (2 votes)
Please Sign up or sign in to vote.
2.33 (2 votes)
20 Sep 2020CPOL
Finding out whether it is faster to retrieve data by reference parameter or returning a reference in C++
Benchmark to find the faster method to retrieve data by reference parameter or returning a reference

The example code is hosted at Github.

Introduction

In C++, there are two options to retrieve data from a object: either passing a reference parameter to be filled up or returning a reference. The former is making a copy of data (which may be what the developer wants) while the latter is returning a memory address of the data.

void PassByReference(std::vector<int>& vec)
{
    vec = m_vec;   // make a copy
}

std::vector<int>& ReturnReference()
{
    return m_vec;  // return memory address
}

Since reference in C++ is a syntactic sugar for pointer, the pointer equivalent of the two above functions are presented below. By right, they should have the same performance characteristic.

void PassByPointer(std::vector<int>* vec)
{
    *vec = m_vec;  // make a copy
}

std::vector<int>* ReturnPointer()
{
    return &m_vec; // return memory address
}

Benchmark Code

The code used for benchmark is below:

const int MAX_LOOP = 100000;
timer stopwatch;

{
    stopwatch.start("PassByReference");
    Foo foo;
    std::vector<int> vec;
    for (int i = 0; i < MAX_LOOP; ++i)
    {
        foo.Add(i);
        foo.PassByReference(vec);
    }
    stopwatch.stop();
}
{
    stopwatch.start("ReturnReference");
    Foo foo;
    std::vector<int>* p = nullptr;
    for (int i = 0; i < MAX_LOOP; ++i)
    {
        foo.Add(i);
        p = &foo.ReturnReference();
    }
    stopwatch.stop();
}
{
    stopwatch.start("PassByPointer");
    Foo foo;
    std::vector<int> vec;
    for (int i = 0; i < MAX_LOOP; ++i)
    {
        foo.Add(i);
        foo.PassByPointer(&vec);
    }
    stopwatch.stop();
}
{
    stopwatch.start("ReturnPointer");
    Foo foo;
    std::vector<int>* p = nullptr;
    for (int i = 0; i < MAX_LOOP; ++i)
    {
        foo.Add(i);
        p = foo.ReturnPointer();
    }
    stopwatch.stop();
}

Benchmark Results

The benchmark results for different C++ compilers are presented below:

Visual C++ 2019 16.7 Update on Windows 10 20.04 Update

PassByReference timing:  565ms
ReturnReference timing:    0ms
  PassByPointer timing:  572ms
  ReturnPointer timing:    0ms

G++ 9.3.0 Ubuntu 20.04 WSL2

G++ build command is as follows:

g++ -std=c++11 -O3 TestReference.cpp
PassByReference timing: 8945ms
ReturnReference timing:    0ms
  PassByPointer timing:  598ms
  ReturnPointer timing:    1ms

Clang++ 10.0.0 Ubuntu 20.04 WSL2

Clang++ build command is as follows:

clang++ -std=c++11 -O3 TestReference.cpp
PassByReference timing: 8824ms
ReturnReference timing:    0ms
  PassByPointer timing:  617ms
  ReturnPointer timing:    0ms

History

  • 20th September, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shao Voon Wong
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. CodeProject awarded him a MVP in recognition of his article contributions in 2019. In his spare time, he prefers to writing applications based on 3rd party libraries than rolling out his own. His interest lies primarily in computer graphics, software optimization, concurrency, security and Agile methodologies.

You can reach him by sending a message on CodeProject or at his Coding Tidbit Blog!

Comments and Discussions

 
QuestionReturn by Reference Pin
Manish K. Agarwal21-Sep-20 20:05
MemberManish K. Agarwal21-Sep-20 20:05 
AnswerRe: Return by Reference Pin
Shao Voon Wong21-Sep-20 20:49
mvaShao Voon Wong21-Sep-20 20:49 
QuestionBy reference or by copy... Pin
davercadman21-Sep-20 5:34
Memberdavercadman21-Sep-20 5:34 
AnswerRe: By reference or by copy... Pin
Shao Voon Wong21-Sep-20 20:53
mvaShao Voon Wong21-Sep-20 20:53 
GeneralRe: By reference or by copy... Pin
davercadman22-Sep-20 1:28
Memberdavercadman22-Sep-20 1:28 
QuestionPerformance of push_back Pin
BugDigger20-Sep-20 22:10
MemberBugDigger20-Sep-20 22:10 
AnswerRe: Performance of push_back Pin
Shao Voon Wong21-Sep-20 1:58
mvaShao Voon Wong21-Sep-20 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.