Frumkin

Untitled

Aug 19th, 2021 (edited)
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cstdlib>
  5.  
  6. // yeah this approach looks really unpleasant but it gets the job done much faster than log10
  7. // according to this: https://www.baeldung.com/java-number-of-digits-in-int
  8. std::uint8_t amountOfDigitsInInteger(const std::uint64_t integer) {
  9.     std::uint8_t total = 0;
  10.     std::uint64_t index = 1;
  11.  
  12.     while (index <= integer) {
  13.         total += 1;
  14.         index *= 10;
  15.     }
  16.  
  17.     return total;
  18. }
  19.  
  20. std::vector<std::uint8_t> getDigits(const std::uint64_t integer) {
  21.     const std::uint8_t totalDigits = amountOfDigitsInInteger(integer);
  22.     std::vector<std::uint8_t> digits(totalDigits);
  23.  
  24.     /*
  25.     69420 / 10000 -> 6
  26.     69420 / 1000 -> 69 % 60 -> 9
  27.     69420 / 100 -> 694 % 690 -> 4
  28.     69420 / 10 -> 6942 % 6940 -> 2
  29.     69420 / 1 -> 64920 % 69420 -> 0
  30.     */
  31.  
  32.     std::uint64_t powerOfTen = 1;
  33.  
  34.     // powerOfTen = 10^totalDigits
  35.     for (std::size_t index = 0; index < totalDigits - 1; index += 1) {
  36.         powerOfTen *= 10;
  37.     }
  38.  
  39.     std::uint64_t slice = integer / powerOfTen;
  40.  
  41.     std::uint64_t past = slice;
  42.  
  43.     digits[0] = slice;
  44.     for (std::size_t index = 1; index < totalDigits; index += 1) {
  45.         powerOfTen /= 10;
  46.         slice = integer / powerOfTen;
  47.         past *= 10;
  48.         digits[index] = slice % past;
  49.  
  50.         past += digits[index];
  51.     }
  52.  
  53.  
  54.     return digits;
  55. }
  56.  
  57. std::vector<std::uint8_t> getDigitsString(const std::uint64_t integer) {
  58.     std::string digitsString = std::to_string(integer);
  59.     std::vector<std::uint8_t> digits(digitsString.size());
  60.  
  61.     for (std::size_t index = 0; index < digitsString.size(); index += 1) {
  62.         digits[index] = *(std::uint8_t*)&digitsString[index] - 48; // 48 is the ascii offset for numbers
  63.     }
  64.  
  65.     return digits;
  66. }
  67.  
  68. int main() {
  69.     auto start = std::chrono::high_resolution_clock::now();
  70.  
  71.     constexpr std::size_t ITERATIONS = 10000;
  72.  
  73.    
  74.  
  75.     for (std::size_t index = 0; index < ITERATIONS; index += 1) {
  76.         std::uint64_t random = static_cast<std::uint64_t>(rand()) << 32 | rand(); // generate a 64 bit integer;
  77.         // for 10000 iterations averaged 0.84 seconds
  78.         std::vector<std::uint8_t> d = getDigits(random);
  79.  
  80.         // for 10000 iterations averaged 0.86 seconds
  81.         //std::vector<std::uint8_t> d = getDigitsString(random);
  82.  
  83.         for (std::size_t index = 0; index < d.size(); index += 1) {
  84.             std::cout << (int)d[index] << std::endl;
  85.         }
  86.     }
  87.  
  88.     std::chrono::duration<double> elapsed = std::chrono::high_resolution_clock::now() - start;
  89.  
  90.     std::cout << "Time: " << std::to_string(elapsed.count()) << " seconds." << std::endl;
  91.  
  92.     return 0;
  93. }
  94.  
Add Comment
Please, Sign In to add comment