Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <tchar.h>
  3.  
  4. #include <atomic>
  5. #include <chrono>
  6. #include <experimental/filesystem>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <thread>
  10. #include <vector>
  11.  
  12. namespace fs = std::experimental::filesystem;
  13. using namespace std;
  14.  
  15. std::atomic<size_t> read_completed_count{ 0 };
  16. std::atomic<size_t> read_hash{ 0 };
  17.  
  18. static void ReadWholeFile(const std::wstring& filename) {
  19. char buffer[1024];
  20. ifstream fin(filename, std::ios::binary);
  21.  
  22. do {
  23. fin.read(buffer, 1024);
  24. read_hash.fetch_add(buffer[0]);
  25. //cout << ".";
  26. /* do nothing */
  27. } while (fin.eof() == false);
  28. //cout << endl;
  29. read_completed_count.fetch_add(1);
  30. }
  31.  
  32. void DoSingleThreadRead(const std::vector<std::wstring>& filenames) {
  33. cout << "SingleThread read" << endl;
  34. for (auto& filename : filenames) {
  35. ReadWholeFile(filename);
  36. }
  37. }
  38.  
  39.  
  40. void DoMultiThreadRead(const std::vector<std::wstring>& filenames) {
  41. cout << "MultiThread read" << endl;
  42. std::atomic<size_t> index{ 0 };
  43. auto thread_func = [&filenames, &index]() {
  44. cout << "A Thread started" << endl;
  45. while (true) {
  46. size_t this_index = index.fetch_add(1) - 1;
  47. if (this_index >= filenames.size()) {
  48. return;
  49. }
  50. auto filename = filenames[this_index];
  51. ReadWholeFile(filename);
  52. }
  53. };
  54.  
  55. constexpr size_t kThreadCount = 8;
  56. std::vector<std::thread> threads;
  57. for (size_t i = 0; i < kThreadCount; ++i) {
  58. threads.emplace_back(thread_func);
  59. }
  60. for (auto& thread : threads) {
  61. thread.join();
  62. }
  63. }
  64.  
  65. int main() {
  66. #ifdef _WIN64
  67. cout << "x64";
  68. #else
  69. cout << "x86";
  70. #endif
  71. #ifdef _DEBUG
  72. cout << " DEBUG ";
  73. #else
  74. cout << " RELEASE ";
  75. #endif
  76. std::vector<std::wstring> filenames;
  77. filenames.reserve(1200);
  78. for (auto& p : fs::directory_iterator("E:\\temp")) {
  79. filenames.emplace_back(p.path().c_str());
  80. }
  81.  
  82. auto start_time = std::chrono::steady_clock::now();
  83. DoSingleThreadRead(filenames);
  84. //DoMultiThreadRead(filenames);
  85. auto end_time = std::chrono::steady_clock::now();
  86.  
  87. auto duration = end_time - start_time;
  88. auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
  89. cout << "milliseconds: " << milliseconds << " read_count:" << read_completed_count << endl;
  90. cout << "read hash: " << read_hash << endl;
  91.  
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement