Click here to Skip to main content
15,884,962 members
Articles / Programming Languages / C++
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
20 Sep 2020CPOL 7K   43   1   7
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.

C++
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.

C++
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:

C++
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)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions

 
QuestionReturn by Reference Pin
Manish K. Agarwal21-Sep-20 20:05
Manish 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
davercadman21-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
davercadman22-Sep-20 1:28 
QuestionPerformance of push_back Pin
BugDigger20-Sep-20 22:10
BugDigger20-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.