fork download
  1. #include <iostream>
  2.  
  3. unsigned long concatenateArr_Sone( unsigned* first, unsigned* last )
  4. {
  5. unsigned long value = 0,
  6. ct = 1;
  7.  
  8. while( last-- != first )
  9. {
  10. if( *last > 9 )
  11. {
  12. value += ct * (*last % 10);
  13. *last++ /= 10;
  14. }
  15. else
  16. value += ct * *last;
  17.  
  18. ct *= 10;
  19. }
  20.  
  21. return value;
  22. }
  23.  
  24. unsigned append_number(unsigned x, unsigned y) {
  25. return (y < 10 ? x : append_number(x, y / 10)) * 10 + y % 10;
  26. }
  27.  
  28. #include <numeric>
  29.  
  30. unsigned long concatenateArr_Seldon( unsigned* first, unsigned* last )
  31. {
  32. return std::accumulate(first, last, 0ul, append_number);
  33. }
  34.  
  35. #include <iterator>
  36. #include <random>
  37. #include <algorithm>
  38. #include <iomanip>
  39. #include <chrono>
  40. #include <ctime>
  41.  
  42. unsigned arr[5];
  43.  
  44. template<typename Callable>
  45. void check( std::string const& name, Callable&& Foo )
  46. {
  47. std::cout << '\n' << name << ": " << Foo( std::begin(arr), std::end(arr) ) << ", needed ";
  48.  
  49. using namespace std::chrono;
  50. auto _tp = system_clock::now();
  51.  
  52. for( unsigned ct = 0; ct < 10000000; ++ct )
  53. auto val = Foo( std::begin(arr), std::end(arr) );
  54.  
  55. std::cout << (system_clock::now() - _tp).count() << " ticks";
  56. }
  57.  
  58. int main()
  59. {
  60. std::uniform_int_distribution<unsigned> dist(0, 100);
  61. std::minstd_rand device(time(nullptr));
  62. std::generate( std::begin(arr), std::end(arr), std::bind(dist, device) );
  63.  
  64. std::cout << "Values: ";
  65. std::copy( std::begin(arr), std::end(arr), std::ostream_iterator<unsigned>(std::cout, ", ") );
  66.  
  67. check( "Seldon", concatenateArr_Seldon );
  68. check( "Sone ", concatenateArr_Sone );
  69. }
Success #stdin #stdout 0.8s 2988KB
stdin
Standard input is empty
stdout
Values: 44, 56, 90, 59, 3, 
Seldon: 445690593, needed 648788 ticks
Sone  : 445690593, needed 154826 ticks