Advertisement
aircampro

optional and timer in boost and sprout

Jun 21st, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. // sudo apt-get install libboost-all-dev
  2. // g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.73.0/gcc-head/include -std=c++2b
  3. // clang++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.69.0/clang-7.0.0/include -std=c++11
  4. //
  5. #include <iostream>
  6. #include <string>
  7.  
  8. #define BOOST  // #define SPROUT
  9.  
  10. #if defined(BOOST)
  11. #include <boost/optional.hpp> // boost 1.63.0
  12. #include <cstdlib>
  13. #include <boost/timer/timer.hpp>
  14. #elif defined(SPROUT)
  15. #include <sprout/optional.hpp> // sprout not boost
  16. #endif
  17.  
  18. #if defined(BOOST)
  19. boost::optional<int> f(int n)
  20. {
  21.     return { n };
  22. }
  23. #elif defined(SPROUT)
  24. sprout::optional<int> f(int n)
  25. {
  26.     return { n };
  27. }
  28. #endif
  29.  
  30. int main()
  31. {
  32. #if defined(BOOST)
  33.     const auto n1 = f(23);
  34.  
  35.     if (n1) std::cout << "note its a pointer " << *n1 << std::endl;
  36.  
  37.     const long n=10000000;
  38.     {
  39.         boost::timer::cpu_timer timer;
  40.  
  41.         for (long i = 0; i < n; ++i) {
  42.         // DO 1
  43.         }
  44.  
  45.         std::string result = timer.format();
  46.         std::cout << result << std::endl;
  47.     }
  48.     {
  49.         boost::timer::cpu_timer timer;
  50.  
  51.         for (long i = 0; i < n; ++i) {
  52.         // DO 2
  53.         }
  54.        
  55.         std::string result = timer.format();
  56.         std::cout << result << std::endl;
  57.     }
  58.  
  59. #elif defined(SPROUT)
  60.     const auto n = f(23);
  61.     for (int i : n) std::cout << "the value using sprout " << i << std::endl;
  62. #endif
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement