Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <cassert>
- #include <numeric>
- #include <algorithm>
- #include <random>
- #include <chrono>
- std::vector<uint64_t> generate_data(std::size_t bytes) {
- assert(bytes % sizeof(uint64_t) == 0);
- std::vector<uint64_t> data(bytes / sizeof(uint64_t));
- std::iota(data.begin(), data.end(), 0);
- std::shuffle(data.begin(), data.end(), std::mt19937{std::random_device{}()});
- return data;
- }
- int main(int argc, char *argv[]) {
- int size;
- if (argc == 2)
- size = atoi(argv[1]);
- else {
- std::cout << "Ошибка, связанная с входным прараметром, попробуйте заново!" << std::endl;
- exit(-1);
- }
- const std::size_t kB = 1024;
- const std::size_t MB = 1024 * kB;
- const std::size_t GB = 1024 * MB;
- // генерация данных размера <size> MB
- std::vector<uint64_t> data = generate_data(MB * size);
- // запись в файл
- auto tStart = std::chrono::high_resolution_clock::now();
- auto ofs = std::fstream("data.bin", std::ios::out | std::ios::binary);
- ofs.write((char *)&data[0], MB * size);
- auto tStop = std::chrono::high_resolution_clock::now();
- ofs.close();
- std::cout << "Время записи (" << size << " МБ): " << std::chrono::duration<double>(tStop - tStart).count() << " сек" << std::endl;
- // чтение из файла
- std::streampos fileSize;
- auto ifs = std::fstream("data.bin", std::ios::in | std::ios::binary);
- ifs.seekg(0, std::ios::end);
- fileSize = ifs.tellg();
- ifs.seekg(0, std::ios::beg);
- std::vector<uint64_t> fileData(fileSize);
- tStart = std::chrono::high_resolution_clock::now();
- ifs.read((char*) &fileData[0], fileSize);
- tStop = std::chrono::high_resolution_clock::now();
- ifs.close();
- std::cout << "Время чтения (" << size << " МБ): " << std::chrono::duration<double>(tStop - tStart).count() << " сек" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement