SHARE
TWEET

Untitled

a guest Jan 14th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <chrono>
  3. using namespace std;
  4. using namespace std::chrono;
  5.  
  6. size_t foo = 0;
  7.  
  8. template<int from, int to> struct Loop {
  9.   inline void operator()() { ++foo; Loop<from+1, to>()(); }
  10. };
  11. template<int to> struct Loop<to, to> { void operator()() {} };
  12.  
  13. struct Incrementer { void operator()() { ++foo; } };
  14.  
  15. int main()
  16. {
  17.   auto start = high_resolution_clock::now();
  18.   Loop<0, 10000>()();
  19.   auto stop = high_resolution_clock::now();
  20.   size_t duration1 = duration_cast<microseconds>(stop-start).count();
  21.  
  22.   start = high_resolution_clock::now();
  23.   for(int from=0, to=10000; from < to; ++from) { Incrementer()(); }
  24.   stop = high_resolution_clock::now();
  25.   size_t duration2 = duration_cast<microseconds>(stop-start).count();
  26.  
  27.   cout << "templated: " << duration1 << ", for: " << duration2 << ", foo: " << foo << endl;
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top