accumulate20.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Demonstrates the moving versions of std::accumulate et al,
  3. * introduced in C++20.
  4. *
  5. * To avoid issues with the names being overloaded, they are
  6. * offered in two ways:
  7. *
  8. * a) under namespace cxxomfort::cxxostd::cxx20
  9. * b) with tail argument of type "numeric_move_tag"
  10. *
  11. * */
  12. #include <cxxomfort/base.hpp>
  13. #include <cxxomfort/algorithm.hpp>
  14. #include <cxxomfort/numeric.hpp>
  15. #include <cxxomfort/type_traits.hpp>
  16. #include <cxxomfort/string.hpp>
  17. #include <cassert>
  18. #include <cstddef> // std::byte
  19. #include <vector>
  20. #include <iostream>
  21. #include <ctime>
  22. const unsigned CALLS=10;
  23. int main () {
  24. using namespace std;
  25. vector<string> V(2000);
  26. for(unsigned i=0; i < V.size(); ++i) {
  27. V[i]= to_string(i+1) + "*";
  28. }
  29. string scopy;
  30. clock_t timec[2] = {clock(), 0 };
  31. for (unsigned t= 0; t<CALLS; ++t) {
  32. using std::accumulate;
  33. scopy= accumulate(begin(V), end(V), string() );
  34. }
  35. timec[1]= clock();
  36. //cout<< scopy<< endl;
  37. cout<< "normal std version : "<< (timec[1] - timec[0])<< endl;
  38. cout<< endl;
  39. // access to move versions
  40. // a) via a tag argument, "numeric_move_tag"
  41. clock_t time_numtag[2] = {clock(), 0 };
  42. for (unsigned t= 0; t<CALLS; ++t) {
  43. using namespace cxxomfort::fix;
  44. //using cxxomfort::fix::numeric_move_tag;
  45. //using cxxomfort::fix::accumulate;
  46. scopy= accumulate(begin(V), end(V), string(), numeric_move_tag());
  47. }
  48. time_numtag[1]= clock();
  49. cout<< "cxxo with numeric_move_tag : "<< (time_numtag[1] - time_numtag[0])<< endl;
  50. cout<< endl;
  51. // access to move versions
  52. // b) via qualified name
  53. clock_t timens[2] = {clock(), 0 };
  54. for (unsigned t= 0; t<CALLS; ++t) {
  55. scopy= cxxomfort::cxxostd::numeric20::accumulate(begin(V), end(V), string() );
  56. }
  57. timens[1]= clock();
  58. cout<< "cxxo with numeric20:: : "<< (timens[1] - timens[0])<< endl;
  59. cout<< endl;
  60. #if 0
  61. // access to move versions
  62. // c) via a different name
  63. clock_t timedi[2] = {clock(), 0 };
  64. for (unsigned t= 0; t<CALLS; ++t) {
  65. using cxxomfort::fix::move_accumulate;
  66. scopy= cxxomfort::fix::move_accumulate(begin(V), end(V), string() );
  67. }
  68. timedi[1]= clock();
  69. cout<< "cxxo with numeric20:: : "<< (timedi[1] - timedi[0])<< endl;
  70. cout<< endl;
  71. #endif
  72. }