Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Gen 4kk int / long long / double and write to file
- File: gen2.cpp
- Command: c++ -std=c++14 -O2 -Wall -Wextra -pedantic -Wconversion -Wshadow -fmax-errors=1 gen2.cpp -o gen2.exe
- */
- #include <stdio.h>
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <cstdlib>
- #include <ctime>
- #include <cassert>
- #include <vector>
- typedef long long ll;
- ll get_ll() {
- ll answ = 0;
- for (int i = 0; i < 6; ++i) (answ *= 1000) += std::rand() % 1000;
- assert(0 <= answ && answ <= 1e18L);
- return answ;
- }
- int main() {
- const int n = 4*1000*1000;
- // Gen vector of 4kk integers:
- std::vector<int> arr_int(n);
- for (auto& it : arr_int) {
- it = int(get_ll() % (int)1e9);
- }
- // Gen vector of 4kk long long:
- std::vector<ll> arr_ll(n);
- for (auto& it : arr_ll) {
- it = get_ll();
- }
- // Gen vector of 4kk double:
- std::vector<double> arr_dbl(n);
- for (auto& it : arr_dbl) {
- it = (double)get_ll() / 1e9;
- }
- double time[4][3] = {}; std::ofstream fout;
- // --- ofstream output
- // Write vector of integers:
- double t = clock();
- fout.open("file_int.txt");
- fout << arr_int.size() << '\n';
- for (auto& it : arr_int) fout << it << ' ';
- fout << '\n';
- fout.close();
- time[2][0] = (clock() - t) / CLOCKS_PER_SEC;
- // Write vector of long longs:
- t = clock();
- fout.open("file_ll.txt");
- fout << arr_ll.size() << '\n';
- for (auto& it : arr_ll) fout << it << ' ';
- fout << '\n';
- fout.close();
- time[2][1] = (clock() - t) / CLOCKS_PER_SEC;
- // Write vector of doubles:
- t = clock();
- fout.open("file_dbl.txt");
- fout << arr_dbl.size() << '\n';
- for (auto& it : arr_dbl) fout << it << ' ';
- fout << '\n';
- fout.close();
- time[2][2] = (clock() - t) / CLOCKS_PER_SEC;
- // --- ifstream input
- // Input vector of integers:
- t = clock();
- std::ifstream fin("file_int.txt");
- int size; fin >> size;
- std::vector<int> temp_int(size);
- for (auto& it : temp_int) {
- fin >> it;
- }
- fin.close();
- time[0][0] = (clock() - t) / CLOCKS_PER_SEC;
- // Input vector of long long:
- t = clock();
- fin.open("file_ll.txt");
- fin >> size;
- std::vector<ll> temp_ll(size);
- for (auto& it : temp_ll) {
- fin >> it;
- }
- fin.close();
- time[0][1] = (clock() - t) / CLOCKS_PER_SEC;
- // Input vector of doubles:
- t = clock();
- fin.open("file_dbl.txt");
- fin >> size;
- std::vector<double> temp_dbl(size);
- for (auto& it : temp_dbl) {
- fin >> it;
- }
- fin.close();
- time[0][2] = (clock() - t) / CLOCKS_PER_SEC;
- // scanf / printf input
- t = clock();
- auto file = fopen("file_int.txt", "rt");
- fscanf(file, "%d", &size);
- temp_int.resize(size);
- for (auto& it : temp_int) fscanf(file, "%d", &it);
- fclose(file);
- time[1][0] = (clock() - t) / CLOCKS_PER_SEC;
- t = clock();
- file = fopen("file_ll.txt", "rt");
- fscanf(file, "%d", &size);
- temp_ll.resize(size);
- for (auto& it : temp_ll) fscanf(file, "%lld", &it);
- fclose(file);
- time[1][1] = (clock() - t) / CLOCKS_PER_SEC;
- t = clock();
- file = fopen("file_dbl.txt", "rt");
- fscanf(file, "%d", &size);
- temp_dbl.resize(size);
- for (auto& it : temp_dbl) fscanf(file, "%lf", &it);
- fclose(file);
- time[1][2] = (clock() - t) / CLOCKS_PER_SEC;
- // scanf / printf output
- t = clock();
- file = fopen("file_int.txt", "wt");
- fprintf(file, "%d\n", size);
- temp_int.resize(size);
- for (auto& it : temp_int) fprintf(file, "%d", it);
- fclose(file);
- time[3][0] = (clock() - t) / CLOCKS_PER_SEC;
- t = clock();
- file = fopen("file_ll.txt", "wt");
- fprintf(file, "%d\n", size);
- temp_ll.resize(size);
- for (auto& it : temp_ll) fprintf(file, "%lld", it);
- fclose(file);
- time[3][1] = (clock() - t) / CLOCKS_PER_SEC;
- t = clock();
- file = fopen("file_dbl.txt", "wt");
- fprintf(file, "%d\n", size);
- temp_dbl.resize(size);
- for (auto& it : temp_dbl) fprintf(file, "%f", it);
- fclose(file);
- time[3][2] = (clock() - t) / CLOCKS_PER_SEC;
- std::cout << "\n---------- Cygwin x64 8.1 - 4kk I/O speed test --------";
- std::cout << "\n-------------------------------------------------------";
- std::cout << "\n type | ifstream | scanf | ofstream | printf |";
- std::cout << "\n-------------------------------------------------------";
- std::cout << "\n int |"; for (int i = 0; i < 4; ++i) std::cout << std::setprecision(3) << std::setw(9) << std::fixed << time[i][0] << " |";
- std::cout << "\nlong long |"; for (int i = 0; i < 4; ++i) std::cout << std::setprecision(3) << std::setw(9) << std::fixed << time[i][1] << " |";
- std::cout << "\n double |"; for (int i = 0; i < 4; ++i) std::cout << std::setprecision(3) << std::setw(9) << std::fixed << time[i][2] << " |";
- std::cout << "\n-------------------------------------------------------";
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement