Skip to content

Instantly share code, notes, and snippets.

@pileon
Created September 19, 2016 13:36
Show Gist options
  • Save pileon/6c280f27415246c292540fd3a5ce6397 to your computer and use it in GitHub Desktop.
Save pileon/6c280f27415246c292540fd3a5ce6397 to your computer and use it in GitHub Desktop.
Shows how to generate a file with 128 rows of 3300 random floating point values, then load it back in
#include <iostream>
#include <vector>
#include <sstream>
#include <random>
#include <fstream>
void save_random_data();
void read_random_data();
int main()
{
{
auto start = std::chrono::high_resolution_clock::now();
save_random_data();
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Saving data: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.0 << " seconds\n";
}
{
auto start = std::chrono::high_resolution_clock::now();
read_random_data();
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Reading data: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.0 << " seconds\n";
}
}
void save_random_data()
{
constexpr size_t no_rows = 128;
constexpr size_t no_cols = 3300;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-1, 1);
std::ofstream output("file.txt");
for (size_t i = 0; i < no_rows; ++i)
{
for (size_t j = 0; j < no_cols; ++j)
{
output << ' ' << dis(gen);
}
output << '\n';
}
}
void read_random_data()
{
std::vector<std::vector<double>> data(128, std::vector<double>(3300));
std::ifstream input("file.txt");
std::stringstream input_buffer;
input_buffer << input.rdbuf();
std::string line;
for (size_t i = 0; std::getline(input_buffer, line); ++i)
{
std::istringstream iss(line);
size_t j = 0;
double v;
while (iss >> v)
data[i][j++] = v;
}
}
@pileon
Copy link
Author

pileon commented Sep 19, 2016

Writing seems consistent to hover a little over 0.2 seconds.
Reading have in my simple testing been between 0.3 and 0.5 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment