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;
}