简体   繁体   中英

C-style Arrays vs std::vector using std::vector::at, std::vector::operator[], and iterators

I have an application where I have to move data from one array ( input ) to another array ( output ), using a third array with a list of destination indexes ( map ). In a simplified way, I want to do something like output[i] = input[ map[i] ] .

I did the following test to try to estimate which will give me better performance between using C-style arrays and std::vector, and in the case of std::vector using different operators as well as iterators. I know that operator std::vector::at() has a performance penalty as it does boundary checking, what I wanted to estimate how much performance hit that would be in order to decide if it is worthy.

I wrote an example application where I move data using different structures and operators. I used gprof to profile it under Linux.

This is the application source code ( test.cpp ):

#include <iostream>
#include <vector>
#include <assert.h>
#include <limits.h>

// Input and output vector size
const std::size_t vector_size    = 4096;
// Size of the map vector. This value must be
// <= 'vector_size'
const std::size_t map_size      = 2000;
// Number of iteration for each algorithm
const std::size_t num_iterations = 1000000;

// Algorithms
void __attribute__ ((noinline)) map_c_array(int *in, std::size_t *map, int *out)
{
    for (std::size_t j {0}; j < map_size; j++)
        out[j] = in[map[j]];
}

void __attribute__ ((noinline)) map_vector_v1(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::size_t j {0};
    for (auto const& m : map)
        out.at(j++) = in.at(m);
}

void __attribute__ ((noinline)) map_vector_v2(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    for (std::size_t j{0}; j < map_size; ++j)
        out.at(j) = in.at(map.at(j));
}

void __attribute__ ((noinline)) map_vector_v3(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::size_t j {0};
    for (auto const& m : map)
        out[j++] = in[m];
}

void __attribute__ ((noinline)) map_vector_v4(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    for (std::size_t j{0}; j < map_size; ++j)
        out[j] = in[map[j]];
}

void __attribute__ ((noinline)) map_vector_v5(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::vector<int>::const_iterator inIt  { in.begin() };
    std::vector<int>::iterator       outIt { out.begin() };
    for (std::vector<std::size_t>::const_iterator mapIt { map.begin() }; mapIt != map.end(); ++mapIt)
        *outIt++ = *(inIt + *mapIt);
}

void __attribute__ ((noinline)) map_vector_v6(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::vector<int>::const_iterator inIt  { in.begin() };
    std::vector<int>::iterator       outIt { out.begin() };
    for (auto const& m : map)
        *outIt++ = *(inIt + m);
}

// Main program
int main(int argc, char *argv[])
{
    // Run the algorithm based on vectors
    for (std::size_t k {0}; k < 6; ++k)
    {
        // Input vector. It is of size = 'vector_size'
        std::vector<int> in(vector_size, 0);

        // Output vector. It is of size = 'vector_size'
        std::vector<int> out(vector_size, 0);

        // Mask Vector. I want to do out[i] = in[ map[i] ]
        // Its values are indexes of the 'in' vector, so they all need
        // to be less than or equal to 'vector_size'
        // It is of size = 'map_size'. To each value in this vector there will
        // be a corresponding value in the 'out' vector. So, 'map_size' need to
        // be less than or equal to 'vector_size'.
        std::vector<std::size_t> map(map_size, 0);


        // Fill input vector with random numbers
        for (std::size_t i {0}; i < vector_size; ++i)
            in.at(i) = static_cast<int>( static_cast<float>(rand())/RAND_MAX * INT_MIN );

        // Fill the map vector with random number, not greater that the
        // maximum size of the in and out vectors.
        for (std::size_t i {0}; i < map_size; ++i)
            map.at(i) = static_cast<std::size_t>( static_cast<float>(rand())/RAND_MAX * vector_size );

        // Copy the values using each algorithm
        switch (k)
        {
            case 0:
                for (std::size_t i {0}; i < num_iterations; ++i )
                {
                    map_vector_v1(in, map, out);

                    // Verify that the values were copied correctly
                    for (std::size_t i {0}; i < map_size; ++i)
                      assert( out[i] == in[map[i]] );
                }
                break;
            case 1:
                for (std::size_t i {0}; i < num_iterations; ++i )
                {
                    map_vector_v2(in, map, out);

                    // Verify that the values were copied correctly
                    for (std::size_t i {0}; i < map_size; ++i)
                      assert( out[i] == in[map[i]] );
                }
                break;
            case 2:
                for (std::size_t i {0}; i < num_iterations; ++i )
                {
                    map_vector_v3(in, map, out);

                    // Verify that the values were copied correctly
                    for (std::size_t i {0}; i < map_size; ++i)
                      assert( out[i] == in[map[i]] );
                }
                break;
            case 3:
                for (std::size_t i {0}; i < num_iterations; ++i )
                {
                    map_vector_v4(in, map, out);

                    // Verify that the values were copied correctly
                    for (std::size_t i {0}; i < map_size; ++i)
                      assert( out[i] == in[map[i]] );
                }
                break;
            case 4:
                for (std::size_t i {0}; i < num_iterations; ++i )
                {
                    map_vector_v5(in, map, out);

                    // Verify that the values were copied correctly
                    for (std::size_t i {0}; i < map_size; ++i)
                      assert( out[i] == in[map[i]] );
                }
                break;
            case 5:
                for (std::size_t i {0}; i < num_iterations; ++i )
                {
                    map_vector_v6(in, map, out);

                    // Verify that the values were copied correctly
                    for (std::size_t i {0}; i < map_size; ++i)
                      assert( out[i] == in[map[i]] );
                }
                break;
        }

    }

        // Finally, run the algorithm based on C arrays
    {
        // Input vector. It is of size = 'vector_size'
        int in[vector_size];

        // Output vector. It is of size = 'vector_size'
        int out[vector_size];

        // Mask Vector. I want to do out[i] = in[ map[i] ]
        // Its values are indexes of the 'in' vector, so they all need
        // to be less than or equal to 'vector_size'
        // It is of size = 'map_size'. To each value in this vector there will
        // be a corresponding value in the 'out' vector. So, 'map_size' need to
        // be less than or equal to 'vector_size'.
        std::size_t map[map_size];


        // Fill input vector with random numbers
        for (std::size_t i {0}; i < vector_size; ++i)
            in[i] = static_cast<int>( static_cast<float>(rand())/RAND_MAX * INT_MIN );

        // Fill the map vector with random number, not greater that the
        // maximum size of the in and out vectors.
        for (std::size_t i {0}; i < map_size; ++i)
            map[i] = static_cast<std::size_t>( static_cast<float>(rand())/RAND_MAX * vector_size );

        for (std::size_t i {0}; i < num_iterations; ++i)
        {
            map_c_array(in, map, out);

            // Verify that the values were copied correctly
            for (std::size_t i {0}; i < map_size; ++i)
              assert( out[i] == in[map[i]] );
        }

    }
}

(Note: I used the noinline attribute to avoid the compiler to inline my functions, as I want to see then in gprof ).

I compiled it with:

g++ -o test test.cpp -Wall -g -O3 -pg

Then I got the profile with:

gprof test

And this is the result:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
 35.42      3.29     3.29                             map_vector_v2(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
 17.17      4.89     1.60                             map_vector_v1(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
 11.34      5.94     1.05                             map_vector_v3(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
 10.37      6.90     0.96                             map_vector_v5(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
  9.72      7.81     0.90                             map_vector_v4(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
  8.64      8.61     0.80                             map_c_array(int*, unsigned long*, int*)
  7.67      9.32     0.71                             map_vector_v6(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
  0.00      9.32     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z11map_c_arrayPiPmS_

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 0.11% of 9.32 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     35.3    3.29    0.00                 map_vector_v2(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [1]
-----------------------------------------------
                                                 <spontaneous>
[2]     17.1    1.60    0.00                 map_vector_v1(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [2]
-----------------------------------------------
                                                 <spontaneous>
[3]     11.3    1.05    0.00                 map_vector_v3(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [3]
-----------------------------------------------
                                                 <spontaneous>
[4]     10.3    0.96    0.00                 map_vector_v5(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [4]
-----------------------------------------------
                                                 <spontaneous>
[5]      9.7    0.90    0.00                 map_vector_v4(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [5]
-----------------------------------------------
                                                 <spontaneous>
[6]      8.6    0.80    0.00                 map_c_array(int*, unsigned long*, int*) [6]
-----------------------------------------------
                                                 <spontaneous>
[7]      7.6    0.71    0.00                 map_vector_v6(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [7]
-----------------------------------------------
                0.00    0.00       1/1           __libc_csu_init [21]
[15]     0.0    0.00    0.00       1         _GLOBAL__sub_I__Z11map_c_arrayPiPmS_ [15]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index  A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function is in the table.

     % time This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self   This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
        function by its children.

     called This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name   The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the function into this parent.

     children   This is the amount of time that was propagated from
        the function's children into this parent.

     called This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name   This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the child into the function.

     children   This is the amount of time that was propagated from the
        child's children to the function.

     called This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name   This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


Index by function name

  [15] _GLOBAL__sub_I__Z11map_c_arrayPiPmS_ (test.cpp) [1] map_vector_v2(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [4] map_vector_v5(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
   [6] map_c_array(int*, unsigned long*, int*) [3] map_vector_v3(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [7] map_vector_v6(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
   [2] map_vector_v1(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&) [5] map_vector_v4(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)

I saw:

  • A notable performance impact, around 60% slower, when using std::vector::at() respect to std::vector:operator[] ( map_vector_v1 vs map_vector_v3 ).
  • It seems like using iterators is around 30% faster than using std::vector::operator[] ( map_vector_v6 vs map_vector_v3 ).
  • I was a little surprise that map_vector_v6 was slightly faster that using C-style arrays ( map_c_array ).
  • I thought that map_vector_v5 and map_vector_v6 where equivalent, so I was surprise that map_vector_v6 was faster.

My initial idea was to use map_vector_v1 . But now I think I will go with map_vector_v6 making sure that my map vector's values are not out of bounds.

I wanted to share this results in case it can help some else, or in case I'm doing something wrong which is affecting my results.

Note: I'm compiling and running this code in Ubuntu 18.04, with:

$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gprof --version
GNU gprof (GNU Binutils for Ubuntu) 2.30
Based on BSD gprof, copyright 1983 Regents of the University of California.
This program is free software.  This program has absolutely no warranty.

EDIT:

Thank you for all your comments. I did some changes to my code, like generating new map and input vectors in each iteration, and do some operations in the output vector at the end of each iteration to avoid it to be optimized out. I also added a second second C-style version which uses pointers, which turned out to be faster.

Here is the updated code.

#include <iostream>
#include <vector>
#include <assert.h>
#include <limits.h>
#include <math.h>

// Input and output vector size
const std::size_t vector_size    = 4096;
// Size of the map vector. This value must be
// <= 'vector_size'
const std::size_t map_size      = 2000;
// Number of iteration for each algorithm
const std::size_t num_iterations = 1000000;

// Algorithms
void __attribute__ ((noinline)) map_c_array(int *in, std::size_t *map, int *out)
{
    for (std::size_t j {0}; j < map_size; j++)
            out[j] = in[map[j]];
}

void __attribute__ ((noinline)) map_c_array_v2(int *in, std::size_t *map, int *out)
{
    for (std::size_t j {0}; j < map_size; j++)
            *out++ = *(in + *map++);
}

void __attribute__ ((noinline)) map_vector_v1(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::size_t j {0};
    for (auto const& m : map)
        out.at(j++) = in.at(m);
}

void __attribute__ ((noinline)) map_vector_v2(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    for (std::size_t j{0}; j < map_size; ++j)
        out.at(j) = in.at(map.at(j));
}

void __attribute__ ((noinline)) map_vector_v3(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::size_t j {0};
    for (auto const& m : map)
        out[j++] = in[m];
}

void __attribute__ ((noinline)) map_vector_v4(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    for (std::size_t j{0}; j < map_size; ++j)
        out[j] = in[map[j]];
}

void __attribute__ ((noinline)) map_vector_v5(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::vector<int>::const_iterator inIt  { in.begin() };
    std::vector<int>::iterator       outIt { out.begin() };
    for (std::vector<std::size_t>::const_iterator mapIt { map.begin() }; mapIt != map.end(); ++mapIt)
        *outIt++ = *(inIt + *mapIt);
}

void __attribute__ ((noinline)) map_vector_v6(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::vector<int>::const_iterator inIt  { in.begin() };
    std::vector<int>::iterator       outIt { out.begin() };
    for (auto const& m : map)
        *outIt++ = *(inIt + m);
}

// Main program
int main(int argc, char *argv[])
{
    // Run algorithms based on vectors
    for (std::size_t k {0}; k < 6; ++k)
    {
        // Run 'num_itertions' iteration for each algorithm
        for (std::size_t i {0}; i < num_iterations; ++i )
        {
            // Input vector. It is of size = 'vector_size'
            std::vector<int> in(vector_size, 0);

            // Output vector. It is of size = 'vector_size'
            std::vector<int> out(vector_size, 0);

            // Mask Vector. I want to do out[i] = in[ map[i] ]
            // Its values are indexes of the 'in' vector, so they all need
            // to be less than or equal to 'vector_size'
            // It is of size = 'map_size'. To each value in this vector there will
            // be a corresponding value in the 'out' vector. So, 'map_size' need to
            // be less than or equalt to 'vector_size'.
            std::vector<std::size_t> map(map_size, 0);


            // Fill input vector with random numbers
            for (std::size_t i {0}; i < vector_size; ++i)
                in.at(i) = static_cast<int>( static_cast<float>(rand())/RAND_MAX * INT_MIN );

            // Fill the map vector with random number, not greater that the
            // maximum size of the in and out vectors.
            for (std::size_t i {0}; i < map_size; ++i)
                map.at(i) = static_cast<std::size_t>( static_cast<float>(rand())/RAND_MAX * ( vector_size - 1 ) );

            // Copy the values using each algorithm
            switch (k)
            {
                case 0:
                    map_vector_v1(in, map, out);
                    break;
                case 1:
                    map_vector_v2(in, map, out);
                    break;
                case 2:
                    map_vector_v3(in, map, out);
                    break;
                case 3:
                    map_vector_v4(in, map, out);
                    break;
                case 4:
                    map_vector_v5(in, map, out);
                    break;
                case 5:
                    map_vector_v6(in, map, out);
                    break;
            }

            // Verify that the values were copied correctly
            for (std::size_t i {0}; i < map_size; ++i)
                assert( out[i] == in[map[i]] );

            // Do some operation in the data
            for (std::size_t i {0}; i < map_size; ++i)
                out[i] += rand();
        }

    }


    // Run algorithms based on C arrays
    {
        // Run the algorithm based on vectors
        for (std::size_t k {0}; k < 2; ++k)
        {
            for (std::size_t i {0}; i < num_iterations; ++i)
            {
                // Input vector. It is of size = 'vector_size'
                int in[vector_size];

                // Output vector. It is of size = 'vector_size'
                int out[vector_size];

                // Mask Vector. I want to do out[i] = in[ map[i] ]
                // Its values are indexes of the 'in' vector, so they all need
                // to be less than or equal to 'vector_size'
                // It is of size = 'map_size'. To each value in this vector there will
                // be a corresponding value in the 'out' vector. So, 'map_size' need to
                // be less than or equalt to 'vector_size'.
                std::size_t map[map_size];


                // Fill input vector with random numbers
                for (std::size_t i {0}; i < vector_size; ++i)
                    in[i] = static_cast<int>( static_cast<float>(rand())/RAND_MAX * INT_MIN );

                // Fill the map vector with random number, not greater that the
                // maximum size of the in and out vectors.
                for (std::size_t i {0}; i < map_size; ++i)
                    map[i] = static_cast<std::size_t>( static_cast<float>(rand())/RAND_MAX * ( vector_size - 1 ) );

                // Copy the values using each algorithm
                switch (k)
                {
                    case 0:
                        map_c_array(in, map, out);
                        break;
                    case 1:
                        map_c_array_v2(in, map, out);
                        break;
                }

                // Verify that the values were copied correctly
                for (std::size_t i {0}; i < map_size; ++i)
                    assert( out[i] == in[map[i]] );

                // Do some operation in the data
                for (std::size_t i {0}; i < map_size; ++i)
                    out[i] += rand();
            }
        }

    }
}

And this is the result I see now:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
 30.55      3.88     3.88                             map_vector_v2(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
 17.76      6.14     2.26                             map_vector_v1(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
 10.66      7.49     1.35                             map_c_array(int*, unsigned long*, int*)
  9.08      8.65     1.15                             map_vector_v5(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
  8.92      9.78     1.13                             map_vector_v3(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
  7.89     10.78     1.00                             map_vector_v4(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
  7.89     11.79     1.00                             map_vector_v6(std::vector<int, std::allocator<int> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<int, std::allocator<int> >&)
  7.58     12.75     0.96                             map_c_array_v2(int*, unsigned long*, int*)
  0.00     12.75     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z11map_c_arrayPiPmS_

Thank you all for your comments.

I just want to summarize my findings.

According to the measurements I took as describe above:

  • These 3 forms presented the fastest execution time in the group:
void map_c_array_v2(int *in, std::size_t *map, int *out)
{
    for (std::size_t j {0}; j < map_size; j++)
            *out++ = *(in + *map++);
}

void map_vector_v4(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    for (std::size_t j{0}; j < map_size; ++j)
        out[j] = in[map[j]];
}

void map_vector_v6(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::vector<int>::const_iterator inIt  { in.begin() };
    std::vector<int>::iterator       outIt { out.begin() };
    for (auto const& m : map)
        *outIt++ = *(inIt + m);
}
  • They were followed by these 2 forms, which were ~10-15% slower. They use additional variables to control the loops:
void  map_vector_v3(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::size_t j {0};
    for (auto const& m : map)
        out[j++] = in[m];
}

void map_vector_v5(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::vector<int>::const_iterator inIt  { in.begin() };
    std::vector<int>::iterator       outIt { out.begin() };
    for (std::vector<std::size_t>::const_iterator mapIt { map.begin() }; mapIt != map.end(); ++mapIt)
        *outIt++ = *(inIt + *mapIt);
}
  • Then this form was about 35% slower. It is very similar to the faster map_vector_v4 , with the difference that map_vector_v4 uses const references:
void map_c_array(int *in, std::size_t *map, int *out)
{
    for (std::size_t j {0}; j < map_size; j++)
            out[j] = in[map[j]];
}
  • The this form was more that 2x slower. This one uses the std::vector::at() operator which does additional boundary checking:
void map_vector_v1(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    std::size_t j {0};
    for (auto const& m : map)
        out.at(j++) = in.at(m);
}
  • And finally this was the slowest, almost 4x slower. This one also uses the std::vector::at() operator as in the previous case, but it uses more of them (3 times instead of 2 times)
void map_vector_v2(const std::vector<int> &in, const std::vector<std::size_t> &map, std::vector<int> &out)
{
    for (std::size_t j{0}; j < map_size; ++j)
        out.at(j) = in.at(map.at(j));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM