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 <string>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <chrono>
- #include <random>
- #include <iostream>
- #include <array>
- #include <execution>
- #include <mutex>
- #include <thread>
- using namespace std;
- default_random_engine thread_safe_generate_random_engine()
- {
- static mutex local_mutex;
- lock_guard const lock(local_mutex);
- static random_device r;
- static default_random_engine gen(r());
- static uniform_int_distribution<unsigned> dist;
- return default_random_engine{ dist(gen) };
- }
- string random_str(size_t len)
- {
- constexpr array<char, 63> alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" };
- thread_local default_random_engine gen = thread_safe_generate_random_engine();
- thread_local uniform_int_distribution<size_t> dis(0, alphabet .size()-2); // оба конца диапазона включаются, не учитываем '\0'
- string result;
- result.reserve(len);
- generate_n(
- back_inserter(result),
- len,
- [&]()
- {
- return alphabet.at(dis(gen));
- }
- );
- return result;
- }
- class random_string20 : public string
- {
- public:
- random_string20() noexcept(noexcept(random_str(20)))
- : string(random_str(20))
- {}
- };
- void update(vector<string>& v)
- {
- constexpr array<char, 63> alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" };
- thread_local default_random_engine gen = thread_safe_generate_random_engine();
- thread_local uniform_int_distribution<size_t> dis(0, alphabet.size() - 2); // оба конца диапазона включаются, не учитываем '\0'
- for_each(std::execution::par,
- v.begin(), v.end(),
- [&](auto& s)
- {
- for_each(s.begin(), s.end(),
- [&](auto& c)
- {
- c = alphabet.at(dis(gen));
- }
- );
- }
- );
- }
- size_t test(vector<string>& vector_string100)
- {
- // как будут готовы range, заменить на ranges
- array<int, 1000> const range{};
- auto const begin = chrono::high_resolution_clock::now();
- update(vector_string100);
- auto const end = chrono::high_resolution_clock::now();
- cout << vector_string100.size() << " strings generation time = "
- << chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << '\n';
- return reduce(
- std::execution::par,
- range.begin(), range.end(),
- 0,
- [&](int acc, int) {
- random_string20 const s20;
- bool const found = any_of(
- vector_string100.cbegin(), vector_string100.cend(),
- [&](string const& str100)
- {
- return str100.find(s20) != string::npos;
- }
- );
- return found ? acc + 1 : acc;
- }
- );
- }
- int main()
- {
- auto begin = chrono::high_resolution_clock::now();
- vector<string> vector_string100(100000, string(100, ' '));
- auto end = chrono::high_resolution_clock::now();
- cout << "Memory allocation time: " <<
- chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << '\n';
- for (int i = 0; i < 10; ++i)
- {
- begin = chrono::high_resolution_clock::now();
- size_t const matches = test(vector_string100);
- end = chrono::high_resolution_clock::now();
- cout << matches << " strings matches, time = "
- << chrono::duration_cast<chrono::milliseconds>
- (end - begin).count() << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement