0

im working on a file parser to import some specific type of JSON to R. The implementation in R requires me to have a set of Vectors with the same length. Its working fine and the async makes it pretty fast, but a lot of time is "wasted" (2/3 of total time!) when collecting the future Vector of Vectors of the async.

Do you have an idea how to speed this up?

For me, basically the problem is to just append the "array" of vectors of vectors.

Im new to C++ and it would be awesome to learn some more!

#include <vector>
#include <iostream>
#include <future>

struct longStruct
{
    std::vector<int> namevec;
    std::vector<std::vector<int>> vectorOfVector;
};

longStruct parser()
{
    longStruct myStruct;

    //vectorsize is variable, just here fixed
    //i need the zeros in the result "matrix"
    std::vector<int> vector(10, 0);
    std::vector<std::vector<int>> vectorOfVector;

    for (size_t i = 0; i < 15; i++) //max amount of vectors is fixed
    {
        myStruct.vectorOfVector.push_back(vector);
    }

    for (size_t i = 0; i < 10; i++)
    {
        myStruct.namevec.push_back(1); //populated with strings usually

        for (size_t j = 0; j < 5; j++)
        {
            //Just change value where it has to be changed.
            //Keep initial zeros if there is no value (important)!
            myStruct.vectorOfVector[i][j] = j; 
        }
    }
    return myStruct;
}

int main()
{

    std::vector<std::future<longStruct>> futures;
    longStruct results;

    std::vector<int> v;
    for (int i = 0; i < 15; ++i)
    {
        results.vectorOfVector.push_back(v);
    }

    for (size_t i = 0; i < 5; i++)
    {
        //Start async operations
        futures.emplace_back(std::async(std::launch::async, parser));
    }

    for (auto &future : futures)
    {
        //Merge results of the async operations
        auto result = future.get();

        //For ne non int vectors
        std::copy(result.namevec.begin(), result.namevec.end(), std::back_inserter(results.namevec));

        //And the "nested": This takes so much time!!!
        for (size_t i = 0; i < result.vectorOfVector.size(); i++)
        {
            std::copy(result.vectorOfVector[i].begin(), result.vectorOfVector[i].end(), std::back_inserter(results.vectorOfVector[i]));
        }
    }

    return 1;
}
schlumpel
  • 174
  • 1
  • 8
  • 2
    "Vectors with the same length" -- typically, you're better off creating a single vector with an aggregate instead. In any case, check out the `reserve()` and `swap()` methods, they are faster than copying data around. – Ulrich Eckhardt Oct 14 '21 at 19:56
  • I will! Thank you for that hint! There might be a problem, that each future is not of the same size. In each future value they have the same length, but differ from future to future. (I did not know how to implement random size numbers) – schlumpel Oct 14 '21 at 20:13
  • [Simple example of Ulrich's comment](https://stackoverflow.com/a/2076668/4581301) – user4581301 Oct 14 '21 at 20:27

0 Answers0