fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <chrono>
  4. #include <vector>
  5. #include <random>
  6. #include <algorithm>
  7. #include <cassert>
  8.  
  9. using namespace std;
  10.  
  11. string gen_rand_str(char start_ch,char end_ch,int len)
  12. {
  13. /*Generating random string using Mersene Twister 19937 generator and Uniform discrete distribution.*/
  14.  
  15. /*start seed with time for randomness*/
  16. std::seed_seq seed{(std::chrono::high_resolution_clock::now().time_since_epoch().count())};
  17.  
  18. std::mt19937 mt19937_generator{seed};
  19. std::uniform_int_distribution<int> ui_distribution{start_ch,end_ch};
  20.  
  21. auto generate_len = len;
  22. string rand_str(generate_len,'\0');
  23.  
  24. for(auto& str: rand_str)
  25. str = ui_distribution(mt19937_generator);
  26.  
  27. return rand_str;
  28. }
  29.  
  30. string gen_ascii_data(int rand_size)
  31. {
  32. string rand_data;
  33. char ch_start = '0';
  34. char ch_end = 'z';
  35.  
  36. rand_data += gen_rand_str(ch_start,ch_end,rand_size) + "\n";
  37.  
  38. return rand_data;
  39. }
  40.  
  41. std::vector<uint64_t> gen_bin_data(std::size_t bytes)
  42. {
  43. assert(bytes % sizeof(uint64_t) == 0);
  44. std::vector<uint64_t> data(bytes / sizeof(uint64_t));
  45. std::iota(data.begin(), data.end(), 0);
  46. std::shuffle(data.begin(), data.end(), std::mt19937{ std::random_device{}() });
  47. return data;
  48. }
  49.  
  50. long long file_stream(string file_name,char* data,std::size_t bytes)
  51. {
  52. auto startTime = std::chrono::high_resolution_clock::now();
  53. auto myfile = std::ofstream(file_name,std::ios::binary);
  54. myfile.write(data, bytes);
  55. myfile.close();
  56. auto endTime = std::chrono::high_resolution_clock::now();
  57.  
  58. return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
  59. }
  60.  
  61. long long c_style_io(string file_name,char* data,std::size_t bytes)
  62. {
  63. auto startTime = std::chrono::high_resolution_clock::now();
  64. FILE* file = fopen(file_name.c_str(), "wb");
  65. fwrite(data, 1, bytes, file);
  66. fclose(file);
  67. auto endTime = std::chrono::high_resolution_clock::now();
  68.  
  69. return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
  70. }
  71.  
  72. long long file_stream_no_sync(string file_name,char* data,std::size_t bytes)
  73. {
  74. std::ios_base::sync_with_stdio(false);
  75. auto startTime = std::chrono::high_resolution_clock::now();
  76. auto myfile = std::ofstream(file_name,std::ios::binary);
  77. myfile.write(data, bytes);
  78. myfile.close();
  79. auto endTime = std::chrono::high_resolution_clock::now();
  80.  
  81. return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
  82. }
  83.  
  84. int main () {
  85.  
  86. std::ofstream out("ReadWriteLog.txt");
  87.  
  88. int file_stream_count = 0,c_style_io_count = 0,file_stream_count_no_sync_count = 0;
  89. std::vector<size_t> file_stream_size_vec;
  90. std::vector<size_t> c_style_io_size_vec;
  91. std::vector<size_t> file_stream_count_no_sync_size_vec;
  92.  
  93. for(uint64_t file_size = sizeof(uint64_t); file_size <= (1ULL << 2); file_size <<= 1){
  94.  
  95. auto gen_data = gen_bin_data(file_size);
  96. char *buffer = (char*)&gen_data[0];
  97. auto size_mb = file_size / (1024 * 1024);
  98. out << "\nReading file with size : " << size_mb << " MB."<< std::endl;
  99.  
  100. auto file_stream_time = file_stream("binary.file",buffer,file_size);
  101. auto c_style_io_time = c_style_io("binary.file",buffer,file_size);
  102. auto file_stream_no_sync_time = file_stream_no_sync("binary.file",buffer,file_size);
  103.  
  104. auto min_time = std::min({file_stream_time,c_style_io_time,file_stream_no_sync_time});
  105. string min_str;
  106.  
  107. if(min_time == file_stream_time){
  108. min_str = "file_stream";
  109. file_stream_count++;
  110. file_stream_size_vec.push_back(size_mb);
  111.  
  112. out << "file_stream_time is " << (c_style_io_time - file_stream_time) << " ms ahead of " << "c_style_io_time" << std::endl;
  113. out << "file_stream_time is " << (file_stream_no_sync_time - file_stream_time) << " ms ahead of " << "file_stream_no_sync_time" << std::endl;
  114.  
  115. }
  116.  
  117. else if(min_time == c_style_io_time){
  118. min_str = "c_style_io";
  119. c_style_io_count++;
  120. c_style_io_size_vec.push_back(size_mb);
  121.  
  122. out << "c_style_io_time is " << (file_stream_time - c_style_io_time) << " ms ahead of " << "file_stream_time" << std::endl;
  123. out << "c_style_io_time is " << (file_stream_no_sync_time - c_style_io_time) << " ms ahead of " << "file_stream_no_sync_time" << std::endl;
  124.  
  125. }
  126.  
  127. else if(min_time == file_stream_no_sync_time){
  128. min_str = "file_stream_no_sync";
  129. file_stream_count_no_sync_count++;
  130. file_stream_count_no_sync_size_vec.push_back(size_mb);
  131.  
  132. out << "file_stream_no_sync_time is " << (c_style_io_time - file_stream_no_sync_time) << " ms ahead of " << "c_style_io_time" << std::endl;
  133. out << "file_stream_no_sync_time is " << (file_stream_time - file_stream_no_sync_time) << " ms ahead of " << "file_stream_time" << std::endl;
  134.  
  135. }
  136. out << "Lesser time of " << min_str << " : " << min_time << " ms"<< std::endl;
  137. }
  138.  
  139. out << "\nfile_stream count : " << file_stream_count << " files with file size in MB's : " << std::endl;
  140. for(auto& vec : file_stream_size_vec) out << vec << " MB, "; out << std::endl;
  141.  
  142. out << "\nc_style_io count : " << c_style_io_count << " files with file size in MB's : " << std::endl;
  143. for(auto& vec : c_style_io_size_vec) out << vec << " MB, ";out << std::endl;
  144.  
  145. out << "\nfile_stream_no_sync count : " << file_stream_count_no_sync_count << " files with file size in MB's : " << std::endl;
  146. for(auto& vec : file_stream_count_no_sync_size_vec) out << vec << " MB, ";out << std::endl;
  147. out.close();
  148.  
  149. std::cout << "ReadWrite finished writing Logs to file." << std::endl;
  150. return 0;
  151. }
Success #stdin #stdout 0s 4356KB
stdin
Standard input is empty
stdout
ReadWrite finished writing Logs to file.