Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <tchar.h>
- #include <atomic>
- #include <chrono>
- #include <experimental/filesystem>
- #include <fstream>
- #include <iostream>
- #include <thread>
- #include <vector>
- namespace fs = std::experimental::filesystem;
- using namespace std;
- std::atomic<size_t> read_completed_count{ 0 };
- std::atomic<size_t> read_hash{ 0 };
- static void ReadWholeFile(const std::wstring& filename) {
- char buffer[1024];
- ifstream fin(filename, std::ios::binary);
- do {
- fin.read(buffer, 1024);
- read_hash.fetch_add(buffer[0]);
- //cout << ".";
- /* do nothing */
- } while (fin.eof() == false);
- //cout << endl;
- read_completed_count.fetch_add(1);
- }
- void DoSingleThreadRead(const std::vector<std::wstring>& filenames) {
- cout << "SingleThread read" << endl;
- for (auto& filename : filenames) {
- ReadWholeFile(filename);
- }
- }
- void DoMultiThreadRead(const std::vector<std::wstring>& filenames) {
- cout << "MultiThread read" << endl;
- std::atomic<size_t> index{ 0 };
- auto thread_func = [&filenames, &index]() {
- cout << "A Thread started" << endl;
- while (true) {
- size_t this_index = index.fetch_add(1) - 1;
- if (this_index >= filenames.size()) {
- return;
- }
- auto filename = filenames[this_index];
- ReadWholeFile(filename);
- }
- };
- constexpr size_t kThreadCount = 8;
- std::vector<std::thread> threads;
- for (size_t i = 0; i < kThreadCount; ++i) {
- threads.emplace_back(thread_func);
- }
- for (auto& thread : threads) {
- thread.join();
- }
- }
- int main() {
- #ifdef _WIN64
- cout << "x64";
- #else
- cout << "x86";
- #endif
- #ifdef _DEBUG
- cout << " DEBUG ";
- #else
- cout << " RELEASE ";
- #endif
- std::vector<std::wstring> filenames;
- filenames.reserve(1200);
- for (auto& p : fs::directory_iterator("E:\\temp")) {
- filenames.emplace_back(p.path().c_str());
- }
- auto start_time = std::chrono::steady_clock::now();
- DoSingleThreadRead(filenames);
- //DoMultiThreadRead(filenames);
- auto end_time = std::chrono::steady_clock::now();
- auto duration = end_time - start_time;
- auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
- cout << "milliseconds: " << milliseconds << " read_count:" << read_completed_count << endl;
- cout << "read hash: " << read_hash << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement