Advertisement
Guest User

Untitled

a guest
Jul 19th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <array>
  2. #include <chrono>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <functional>
  6. #include <set>
  7. #include <tuple>
  8.  
  9. using namespace std;
  10. using namespace chrono;
  11.  
  12. const size_t SIZE = 1000;
  13. const double lg5 = log(5) / log(2);
  14.  
  15. typedef unsigned long myint;
  16. typedef array<myint, SIZE> myarray;
  17.  
  18. void use_set(myarray& out) {
  19.     set<myint> s = {1};
  20.     for (auto& x : out) {
  21.         auto it = s.begin();
  22.         x = *it;
  23.         s.erase(it);
  24.         s.insert(x * 2);
  25.         s.insert(x * 5);
  26.     }
  27. }
  28.  
  29. tuple<double, int, int> incr_i(int i, int j) {
  30.     double mindiff = 1;
  31.     int mini = ++i, minj = j;
  32.     double diff = mindiff;
  33.     while (j > 0) {
  34.         --j;
  35.         diff -= lg5;
  36.         while (diff < 0) {
  37.             ++i;
  38.             ++diff;
  39.         }
  40.         if (diff < mindiff) {
  41.             mindiff = diff;
  42.             mini = i;
  43.             minj = j;
  44.         }
  45.     }
  46.     return make_tuple(mindiff, mini, minj);
  47. }
  48.  
  49. tuple<double, int, int> incr_j(int i, int j) {
  50.     double mindiff = lg5;
  51.     int mini = i, minj = ++j;
  52.     double diff = mindiff;
  53.     while (i > 0) {
  54.         --i;
  55.         diff -= 1;
  56.         if (diff < 0) {
  57.             ++j;
  58.             diff += lg5;
  59.         }
  60.         if (diff < mindiff) {
  61.             mindiff = diff;
  62.             mini = i;
  63.             minj = j;
  64.         }
  65.     }
  66.     return make_tuple(mindiff, mini, minj);
  67. }
  68.  
  69.  
  70.  
  71. void use_dbl(myarray& out) {
  72.     int i = 0, j = 0;
  73.     myarray pow2 = {1}, pow5 = {1};
  74.     for (size_t x = 1; x < SIZE; ++x) {
  75.         pow2[x] = pow2[x - 1] * 2;
  76.         pow5[x] = pow5[x - 1] * 5;
  77.     }
  78.     for (auto& x : out) {
  79.         x = pow2[i] * pow5[j];
  80.         double d1, d2;
  81.         int i1, j1, i2, j2;
  82.         tie(d1, i1, j1) = incr_i(i, j);
  83.         tie(d2, i2, j2) = incr_j(i, j);
  84.         if (d1 <= d2) tie(i, j) = tie(i1, j1);
  85.         else          tie(i, j) = tie(i2, j2);
  86.     }
  87. }
  88.  
  89. template<class DurationType = chrono::microseconds, class F>
  90. typename DurationType::rep closure_time(const F& f) {
  91.     typedef high_resolution_clock myclock;
  92.     auto start = myclock::now();
  93.     f();
  94.     auto end = myclock::now();
  95.     return duration_cast<DurationType>(end - start).count();
  96. }
  97.  
  98. int main()
  99. {
  100.     cout << "Print first " << SIZE << " elements of {i,j > 0 | 2^i * 5^j}\n";
  101.    
  102.     myarray using_set;
  103.     cout << "Using set time: ";
  104.     cout << closure_time(bind(use_set, ref(using_set))) << endl;
  105.     for (auto x : using_set)
  106.         cout << x << ' ';
  107.     cout << endl;
  108.    
  109.     myarray using_dbl;
  110.     cout << "Using dbl time: ";
  111.     cout << closure_time(bind(use_dbl, ref(using_dbl))) << endl;
  112.     for (auto x : using_dbl)
  113.         cout << x << ' ';
  114.     cout << endl;
  115.    
  116.    return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement