Advertisement
Guest User

gen2.cpp

a guest
Jul 16th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. /*
  2.     Gen 4kk int / long long / double and write to file
  3.    
  4.     File: gen2.cpp
  5.    
  6.     Command: c++ -std=c++14 -O2 -Wall -Wextra -pedantic -Wconversion -Wshadow -fmax-errors=1 gen2.cpp -o gen2.exe
  7.  
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <fstream>
  14. #include <cstdlib>
  15. #include <ctime>
  16. #include <cassert>
  17. #include <vector>
  18.  
  19. typedef long long ll;
  20.  
  21. ll get_ll() {
  22.     ll answ = 0;
  23.     for (int i = 0; i < 6; ++i) (answ *= 1000) += std::rand() % 1000;
  24.     assert(0 <= answ && answ <= 1e18L);
  25.     return answ;
  26. }
  27.  
  28. int main() {
  29.     const int n = 4*1000*1000;
  30.    
  31.     // Gen vector of 4kk integers:
  32.     std::vector<int> arr_int(n);
  33.     for (auto& it : arr_int) {
  34.         it = int(get_ll() % (int)1e9);
  35.     }
  36.    
  37.     // Gen vector of 4kk long long:
  38.     std::vector<ll> arr_ll(n);
  39.     for (auto& it : arr_ll) {
  40.         it = get_ll();
  41.     }
  42.    
  43.     // Gen vector of 4kk double:
  44.     std::vector<double> arr_dbl(n);
  45.     for (auto& it : arr_dbl) {
  46.         it = (double)get_ll() / 1e9;
  47.     }
  48.    
  49.     double time[4][3] = {}; std::ofstream fout;
  50.     // --- ofstream output
  51.     // Write vector of integers:
  52.     double t = clock();
  53.    
  54.     fout.open("file_int.txt");
  55.    
  56.     fout << arr_int.size() << '\n';
  57.     for (auto& it : arr_int) fout << it << ' ';
  58.     fout << '\n';
  59.    
  60.     fout.close();
  61.    
  62.     time[2][0] = (clock() - t) / CLOCKS_PER_SEC;
  63.    
  64.     // Write vector of long longs:
  65.     t = clock();
  66.    
  67.     fout.open("file_ll.txt");
  68.    
  69.     fout << arr_ll.size() << '\n';
  70.     for (auto& it : arr_ll) fout << it << ' ';
  71.     fout << '\n';
  72.    
  73.     fout.close();
  74.    
  75.     time[2][1] = (clock() - t) / CLOCKS_PER_SEC;
  76.    
  77.     // Write vector of doubles:
  78.     t = clock();
  79.    
  80.     fout.open("file_dbl.txt");
  81.    
  82.     fout << arr_dbl.size() << '\n';
  83.     for (auto& it : arr_dbl) fout << it << ' ';
  84.     fout << '\n';
  85.    
  86.     fout.close();
  87.    
  88.     time[2][2] = (clock() - t) / CLOCKS_PER_SEC;
  89.    
  90.     // --- ifstream input
  91.     // Input vector of integers:
  92.     t = clock();
  93.     std::ifstream fin("file_int.txt");
  94.    
  95.     int size; fin >> size;
  96.     std::vector<int> temp_int(size);
  97.     for (auto& it : temp_int) {
  98.         fin >> it;
  99.     }
  100.     fin.close();
  101.     time[0][0] = (clock() - t) / CLOCKS_PER_SEC;
  102.    
  103.     // Input vector of long long:
  104.     t = clock();
  105.     fin.open("file_ll.txt");
  106.    
  107.     fin >> size;
  108.     std::vector<ll> temp_ll(size);
  109.     for (auto& it : temp_ll) {
  110.         fin >> it;
  111.     }
  112.     fin.close();
  113.     time[0][1] = (clock() - t) / CLOCKS_PER_SEC;
  114.    
  115.     // Input vector of doubles:
  116.     t = clock();
  117.     fin.open("file_dbl.txt");
  118.    
  119.     fin >> size;
  120.     std::vector<double> temp_dbl(size);
  121.     for (auto& it : temp_dbl) {
  122.         fin >> it;
  123.     }
  124.     fin.close();
  125.     time[0][2] = (clock() - t) / CLOCKS_PER_SEC;
  126.    
  127.     // scanf / printf input
  128.     t = clock();
  129.     auto file = fopen("file_int.txt", "rt");
  130.     fscanf(file, "%d", &size);
  131.     temp_int.resize(size);
  132.     for (auto& it : temp_int) fscanf(file, "%d", &it);
  133.     fclose(file);
  134.     time[1][0] = (clock() - t) / CLOCKS_PER_SEC;
  135.    
  136.     t = clock();
  137.     file = fopen("file_ll.txt", "rt");
  138.     fscanf(file, "%d", &size);
  139.     temp_ll.resize(size);
  140.     for (auto& it : temp_ll) fscanf(file, "%lld", &it);
  141.     fclose(file);
  142.     time[1][1] = (clock() - t) / CLOCKS_PER_SEC;
  143.    
  144.     t = clock();
  145.     file = fopen("file_dbl.txt", "rt");
  146.     fscanf(file, "%d", &size);
  147.     temp_dbl.resize(size);
  148.     for (auto& it : temp_dbl) fscanf(file, "%lf", &it);
  149.     fclose(file);
  150.     time[1][2] = (clock() - t) / CLOCKS_PER_SEC;
  151.    
  152.     // scanf / printf output
  153.     t = clock();
  154.     file = fopen("file_int.txt", "wt");
  155.     fprintf(file, "%d\n", size);
  156.     temp_int.resize(size);
  157.     for (auto& it : temp_int) fprintf(file, "%d", it);
  158.     fclose(file);
  159.     time[3][0] = (clock() - t) / CLOCKS_PER_SEC;
  160.    
  161.     t = clock();
  162.     file = fopen("file_ll.txt", "wt");
  163.     fprintf(file, "%d\n", size);
  164.     temp_ll.resize(size);
  165.     for (auto& it : temp_ll) fprintf(file, "%lld", it);
  166.     fclose(file);
  167.     time[3][1] = (clock() - t) / CLOCKS_PER_SEC;
  168.    
  169.     t = clock();
  170.     file = fopen("file_dbl.txt", "wt");
  171.     fprintf(file, "%d\n", size);
  172.     temp_dbl.resize(size);
  173.     for (auto& it : temp_dbl) fprintf(file, "%f", it);
  174.     fclose(file);
  175.     time[3][2] = (clock() - t) / CLOCKS_PER_SEC;
  176.     std::cout << "\n---------- Cygwin x64 8.1 - 4kk I/O speed test --------";
  177.     std::cout << "\n-------------------------------------------------------";
  178.     std::cout << "\n     type | ifstream |    scanf | ofstream |   printf |";
  179.     std::cout << "\n-------------------------------------------------------";
  180.     std::cout << "\n      int |"; for (int i = 0; i < 4; ++i) std::cout << std::setprecision(3) << std::setw(9) << std::fixed << time[i][0] << " |";
  181.     std::cout << "\nlong long |"; for (int i = 0; i < 4; ++i) std::cout << std::setprecision(3) << std::setw(9) << std::fixed << time[i][1] << " |";
  182.     std::cout << "\n   double |"; for (int i = 0; i < 4; ++i) std::cout << std::setprecision(3) << std::setw(9) << std::fixed << time[i][2] << " |";
  183.     std::cout << "\n-------------------------------------------------------";
  184.     std::cout << std::endl;
  185.    
  186.    
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement