-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.cpp
More file actions
76 lines (62 loc) · 1.74 KB
/
10.cpp
File metadata and controls
76 lines (62 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <filesystem>
#include <windows.h>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <atomic>
#include <thread>
namespace fs = std::filesystem;
std::atomic_int lines_count;
std::vector<fs::path> GetAllFiles(const fs::path& dirName)
{
std::vector<fs::path> result;
fs::directory_entry entry{dirName};
if (entry.exists())
{
for (auto const& dir_entry : fs::recursive_directory_iterator(dirName))
if (dir_entry.is_regular_file())
{
result.push_back(dir_entry.path());
}
return result;
}
return std::vector<fs::path>();
}
std::vector<std::string> read_file(const std::string& filename)
{
std::vector<std::string> res;
std::string line;
std::ifstream infile(filename);
while(std::getline(infile, line)){
lines_count++;
res.push_back(line);
}
infile.close();
return res;
}
void LineCounter(const fs::path& f)
{
read_file(f.string());
}
int main()
{
std::vector<std::thread> pool;
SetConsoleOutputCP(CP_UTF8);
fs::path dirName = "C:\\Log";
auto start = std::chrono::high_resolution_clock::now();
auto files = GetAllFiles(dirName);
for (auto const& f:files){
std::cout<<f<<"\n";
pool.emplace_back(LineCounter, f);
}
for (auto& thread: pool)
thread.join();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Время выполнения: " << elapsed.count() << " секунд" << std::endl;
std::cout << "Общее количество строк: " << lines_count << std::endl;
return 0;
}