Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cassert>
  5. #include <numeric>
  6. #include <algorithm>
  7. #include <random>
  8. #include <chrono>
  9.  
  10. std::vector<uint64_t> generate_data(std::size_t bytes) {
  11.     assert(bytes % sizeof(uint64_t) == 0);
  12.     std::vector<uint64_t> data(bytes / sizeof(uint64_t));
  13.     std::iota(data.begin(), data.end(), 0);
  14.     std::shuffle(data.begin(), data.end(), std::mt19937{std::random_device{}()});
  15.     return data;
  16. }
  17.  
  18. int main(int argc, char *argv[]) {
  19.     int size;
  20.     if (argc == 2)
  21.         size = atoi(argv[1]);
  22.     else {
  23.         std::cout << "Ошибка, связанная с входным прараметром, попробуйте заново!" << std::endl;
  24.         exit(-1);
  25.     }
  26.  
  27.     const std::size_t kB = 1024;
  28.     const std::size_t MB = 1024 * kB;
  29.     const std::size_t GB = 1024 * MB;
  30.  
  31.     // генерация данных размера <size> MB
  32.     std::vector<uint64_t> data = generate_data(MB * size);
  33.    
  34.     // запись в файл
  35.     auto tStart = std::chrono::high_resolution_clock::now();
  36.     auto ofs = std::fstream("data.bin", std::ios::out | std::ios::binary);
  37.     ofs.write((char *)&data[0], MB * size);
  38.     auto tStop = std::chrono::high_resolution_clock::now();
  39.     ofs.close();
  40.     std::cout << "Время записи (" << size << " МБ): " << std::chrono::duration<double>(tStop - tStart).count() << " сек" << std::endl;
  41.    
  42.     // чтение из файла
  43.     std::streampos fileSize;
  44.     auto ifs = std::fstream("data.bin", std::ios::in | std::ios::binary);
  45.     ifs.seekg(0, std::ios::end);
  46.     fileSize = ifs.tellg();
  47.     ifs.seekg(0, std::ios::beg);
  48.  
  49.     std::vector<uint64_t> fileData(fileSize);
  50.     tStart = std::chrono::high_resolution_clock::now();
  51.     ifs.read((char*) &fileData[0], fileSize);
  52.     tStop = std::chrono::high_resolution_clock::now();
  53.     ifs.close();
  54.     std::cout << "Время чтения (" << size << " МБ): " << std::chrono::duration<double>(tStop - tStart).count() << " сек" << std::endl;
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement