Created
September 19, 2016 13:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.