C -стильные массивы против std :: vector с использованием std :: vector :: at, std :: vector :: operator [] и итераторов - PullRequest
0 голосов
/ 06 мая 2020

У меня есть приложение, в котором мне нужно переместить данные из одного массива (input) в другой массив (output), используя третий массив со списком индексов назначения (map). Упрощенно, я хочу сделать что-то вроде output[i] = input[ map[i] ].

. Я провел следующий тест, чтобы попытаться оценить, что даст мне лучшую производительность между использованием массивов в стиле C и std :: vector, и в случае std :: vector с использованием разных операторов, а также итераторов. Я знаю, что оператор std::vector::at() имеет снижение производительности, поскольку он выполняет проверку границ, и я хотел оценить, насколько сильно это снизится, чтобы решить, стоит ли это.

Я написал пример приложения, в котором я перемещаю данные, используя разные структуры и операторы. Я использовал gprof, чтобы профилировать его под Linux.

Это исходный код приложения (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]] );
        }

    }
}

(Примечание: я использовал атрибут noinline, чтобы компилятор не встраивал мои функции, так как я хочу см. затем в gprof).

Я скомпилировал его с помощью:

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

Затем я получил профиль с:

gprof test

И вот результат:

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> >&)

Я видел:

  • Заметное влияние на производительность, примерно на 60% медленнее, при использовании std::vector::at() по сравнению с std::vector:operator[] (map_vector_v1 против map_vector_v3) .
  • Кажется, что использование итераторов примерно на 30% быстрее, чем использование std::vector::operator[] (map_vector_v6 против map_vector_v3).
  • Я был немного удивлен, что map_vector_v6 был немного быстрее, чем с использованием массивов в стиле C (map_c_array).
  • Я думал, что map_vector_v5 и map_vector_v6 эквивалентны, поэтому меня удивило, что map_vector_v6 был быстрее.

Моя первоначальная идея заключалась в использовании map_vector_v1. Но теперь я думаю, что сделаю go с map_vector_v6, убедившись, что значения моего map вектора не выходят за рамки.

Я хотел поделиться этими результатами на случай, если это может помочь кому-то другому, или в случае, если я делаю что-то не так, что влияет на мои результаты.

Примечание: я компилирую и запускаю этот код в Ubuntu 18.04 с:

$ 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:

Спасибо за все ваши комментарии. Я внес некоторые изменения в свой код, например, сгенерировал новую карту и входные векторы на каждой итерации, и проделал некоторые операции с выходным вектором в конце каждой итерации, чтобы избежать его оптимизации. Я также добавил вторую вторую версию в стиле C, в которой используются указатели, что оказалось быстрее.

Вот обновленный код.

#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();
            }
        }

    }
}

И вот результат, который я вижу сейчас:

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_

1 Ответ

0 голосов
/ 09 июня 2020

Всем спасибо за комментарии.

Я просто хочу подытожить свои выводы.

Согласно измерениям, которые я сделал, как описано выше:

  • Эти 3 формы показали самое быстрое время выполнения в группе:
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);
}
  • За ними следовали эти 2 формы, которые были на ~ 10-15% медленнее. Они используют дополнительные переменные для управления циклами:
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);
}
  • Тогда эта форма была примерно на 35% медленнее. Он очень похож на более быстрый map_vector_v4, с той разницей, что map_vector_v4 использует const ссылок:
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]];
}
  • Эта форма была более чем в 2 раза медленнее. Здесь используется оператор std::vector::at(), который выполняет дополнительную проверку границ:
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);
}
  • И, наконец, это был самый медленный, почти в 4 раза медленнее. Этот также использует оператор std::vector::at(), как и в предыдущем случае, но использует их больше (3 раза вместо 2 раз)
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));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...