Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a personal academic project. Dear PVS-Studio, please check it.
- // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
- #include <numeric>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <chrono>
- #include <random>
- #include <iostream>
- #include <array>
- #include <mutex>
- #include <thread>
- #include <unordered_set>
- #include <string_view>
- #include <cassert>
- using namespace std;
- void update_str(string& s)
- {
- constexpr array<char, 63> alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" };
- thread_local random_device r;
- thread_local default_random_engine gen(r());
- thread_local uniform_int_distribution<size_t> dis(0, alphabet.size() - 2); // оба конца диапазона включаются, не учитываем '\0'
- for(auto&& c: s)
- {
- c = alphabet.at(dis(gen));
- }
- }
- class random_string20 : public string
- {
- public:
- random_string20()
- {
- resize(20);
- update_str(*this);
- }
- };
- void update_vector(vector<string>& v)
- {
- for_each(
- v.begin(), v.end(),
- [](string& s)
- {
- update_str(s);
- }
- );
- }
- size_t test(vector<string>& vector_string100)
- {
- auto result = size_t(0);
- auto begin = chrono::high_resolution_clock::now();
- update_vector(vector_string100);
- auto end = chrono::high_resolution_clock::now();
- {
- cout << "String generation time: "
- << chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << '\n';
- begin = chrono::high_resolution_clock::now();
- unordered_set<string_view> hash_set;
- hash_set.reserve(81 * vector_string100.size());
- for (auto&& s : vector_string100)
- {
- assert(s.size() >= 19);
- for (size_t i = 0; i < s.size() - 19; ++i)
- {
- hash_set.insert(string_view(&s.at(i), 20));
- }
- }
- end = chrono::high_resolution_clock::now();
- cout << "Hash generation time: "
- << chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << '\n';
- begin = chrono::high_resolution_clock::now();
- // как будут готовы range, заменить на ranges
- array<int, 1000> const range{};
- result = accumulate(
- range.begin(), range.end(),
- size_t(0),
- [&](size_t acc, size_t)
- {
- random_string20 const s20;
- auto const iter = hash_set.find(s20);
- bool const found = (iter != hash_set.end());
- return found ? acc + 1 : acc;
- }
- );
- end = chrono::high_resolution_clock::now();
- cout << "Substring search time: "
- << chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << '\n';
- begin = chrono::high_resolution_clock::now();
- }
- end = chrono::high_resolution_clock::now();
- cout << "Hashmap destructor time: "
- << chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << '\n';
- return result;
- }
- int main()
- {
- for (int i = 0; i < 10; ++i)
- {
- auto const begin = chrono::high_resolution_clock::now();
- vector<string> vector_string100(100000, string(100, ' '));
- size_t const matches = test(vector_string100);
- auto const end = chrono::high_resolution_clock::now();
- cout << matches << " strings matches, time = "
- << chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << "\n\n\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement