Advertisement
Guest User

Untitled

a guest
Apr 11th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3.  
  4. static unsigned long long total = 0;
  5.  
  6. __attribute__((__noinline__)) void by_ref(const std::chrono::system_clock::time_point& t)
  7. {
  8.   total += (std::chrono::system_clock::now() - t).count();
  9. }
  10.  
  11. __attribute__((__noinline__)) void by_value(std::chrono::system_clock::time_point t)
  12. {
  13.   total += (std::chrono::system_clock::now() - t).count();
  14. }
  15.  
  16. int main(void)
  17. {
  18.   std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
  19.   std::cout << "size: " << sizeof(t) << std::endl;
  20.  
  21.   const unsigned long long tries = 10000000;
  22.   auto before = std::chrono::high_resolution_clock::now();
  23.   for (unsigned long long i = 0; i < tries; ++i)
  24.     by_ref(t);
  25.   auto diff = std::chrono::high_resolution_clock::now() - before;
  26.   std::cout << "by_ref: " << diff.count() << std::endl;
  27.  
  28.   before = std::chrono::high_resolution_clock::now();
  29.   for (unsigned long long i = 0; i < tries; ++i)
  30.     by_value(t);
  31.   diff = std::chrono::high_resolution_clock::now() - before;
  32.   std::cout << "by_value: " << diff.count() << std::endl;
  33.  
  34.   std::cout << "total: " << total << std::endl;
  35.  
  36.   return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement