Parallel version, 3 seconds

This commit is contained in:
Pcornat 2021-03-12 22:18:34 +01:00
parent 971acfe372
commit 296f78b404
No known key found for this signature in database
GPG Key ID: 873C3ACCF970C74E

View File

@ -10,9 +10,9 @@
* \param my_string * \param my_string
* \return * \return
*/ */
boost::crc_32_type::value_type getCrc32(const std::string_view my_string) { boost::crc_32_type::value_type getCrc32(const char *__restrict__ const my_string, const std::size_t size) {
boost::crc_32_type result; boost::crc_32_type result;
result.process_bytes(my_string.data(), my_string.length()); result.process_bytes(my_string, size);
return result.checksum(); return result.checksum();
} }
@ -77,30 +77,13 @@ struct NumberBase26 final {
~NumberBase26() noexcept = default; ~NumberBase26() noexcept = default;
/** void operator()(std::size_t n) noexcept {
* \brief Cela permet d'incrémenter le nombre. for (size_t i = data.size() - 1; 0 <= i; --i) {
* \return if (n == 0)
*/ return;
NumberBase26 &operator++() { data[i] = alphabet[n % valMax];
std::uint32_t number{ lut.at(data.back()) + 1 }; // C'est là où se trouve l'incrémentation n /= valMax;
/*
* Tant que la retenue se propage à travers les digit, il faut les mettre à 'A' et
* poursuivre jusqu'à rencontrer un caractère != 'Z'
*/
for (std::size_t i = data.size() - 1; 0 <= i; --i) {
if (retenue) {
number = lut.at(data[i]) + 1;
} }
if (number == valMax) {
data[i] = 'A';
retenue = true;
} else {
data[i] = alphabet[number];
retenue = false;
break;
}
}
return *this;
} }
friend std::ostream &operator<<(std::ostream &os, const NumberBase26 &number_base_26) { friend std::ostream &operator<<(std::ostream &os, const NumberBase26 &number_base_26) {
@ -108,9 +91,6 @@ struct NumberBase26 final {
return os; return os;
} }
private:
//! Signifie la présence d'une retenue.
bool retenue{ false };
}; };
int main() { int main() {
@ -234,30 +214,20 @@ int main() {
constexpr std::size_t maxDig = maxDigits(maxCompute); constexpr std::size_t maxDig = maxDigits(maxCompute);
NumberBase26<maxDig> number_base_26{ alphabet }; NumberBase26<maxDig> number_base_26{ alphabet };
std::vector<std::size_t> crcs;
std::vector<char> results;
results.reserve(maxDig * maxCompute);
crcs.reserve(maxCompute);
const double start = omp_get_wtime(); const double start = omp_get_wtime();
for (size_t i = 0; i < maxCompute; ++i, ++number_base_26) { #pragma omp parallel for firstprivate(number_base_26)
const auto crc = ~(getCrc32(std::string_view{ number_base_26.data.data(), maxDig })); for (size_t i = 0; i < maxCompute; ++i) {
number_base_26(i);
const auto crc = ~(getCrc32(number_base_26.data.data(), number_base_26.data.size()));
if (std::find(cheat_list.cbegin(), cheat_list.cend(), crc) != cheat_list.cend()) { if (std::find(cheat_list.cbegin(), cheat_list.cend(), crc) != cheat_list.cend()) {
crcs.emplace_back(crc); #pragma omp critical
for (const char &j : number_base_26.data) { {
results.emplace_back(j); std::cout << number_base_26 << " : 0x" << std::hex << crc << '\n';
} }
} }
} }
const double stop = omp_get_wtime(); const double stop = omp_get_wtime();
crcs.shrink_to_fit();
results.shrink_to_fit();
for (size_t i = 0; i < crcs.size(); ++i) {
for (size_t j = 0; j < maxDig; ++j) {
std::cout << results[i * maxDig + j];
}
std::cout << " : 0x" << std::hex << crcs[i] << '\n';
}
std::cout << "Temps : " << stop - start << 's' << std::endl; std::cout << "Temps : " << stop - start << 's' << std::endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }