#include <iostream>
#include <vector> // for std::vector
#include <fstream> // for std::fstream
#include <string> // for std::string, std::to_string
#include <chrono> // for std::chrono::*
#include <sstream> // for std::stringstream
#include <future> // for std::async
auto fake_calculation(size_t x, size_t y, size_t d) -> std::vector<float*>;
int main() {
constexpr size_t sizex = 638;
constexpr size_t sizey = 958;
constexpr size_t dim = 8;
std::vector<float*> out = fake_calculation(sizex, sizey, dim);
std::string line = std::to_string(sizex) + "\n" + std::to_string(sizey) + "\n" + std::to_string(dim) + "\n";
std::vector<std::future<std::string> > lines;
// start performance measuring:
auto time_start = std::chrono::high_resolution_clock::now();
for (int d = 0; d < dim; d++)
{ // SPAWN
auto future_line = std::async(std::launch::async,
[&](const size_t local_d) {
// see https://stackoverflow.com/a/58989107
size_t to_reserve = (sizex * sizey) * 13 + 1;
std::string dummy(to_reserve, '\0');
std::stringstream local_ss(dummy);
dummy.clear();
dummy.shrink_to_fit();
/*****************************/
/* SET NUMBER FORMAT HERE */
local_ss.precision(6);
local_ss << std::scientific;
/*****************************/
for (int x = 0; x < sizex * sizey; x++)
{
local_ss << out[local_d][x] << ",";
}
local_ss << "\n";
return local_ss.str();
}, d);
lines.push_back(std::move(future_line));
}
for (auto& fl : lines)
{ // COLLECT
line += fl.get();
}
// end performance measuring:
auto time_end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = time_end - time_start;
std::cout << "conversion took " << elapsed.count() << "ms to finish." << std::endl;
// dumping to file:
std::fstream data;
data.open("perf_test.csv", std::ios::out);
data << line;
data.close();
for (auto pf : out) { // free pointers
delete pf;
}
}
auto fake_calculation(size_t p_x, size_t p_y, size_t p_d)
-> std::vector<float*> {
std::vector<float*> fake_result;
for (size_t d = 0; d < p_d; d++) {
float *x_array = new float [p_x * p_y];
for (size_t x = 0; x < p_x * p_y; x++) {
x_array[x] = 1234.5 * (d+2) * x / 678.9; // something
}
fake_result.push_back(x_array);
}
return fake_result;
}