fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <climits>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <random>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10. unsigned int myConcat(unsigned int& a, unsigned int& b) {
  11. switch( (b >= 10000000) ? 7 :
  12. (b >= 1000000) ? 6 :
  13. (b >= 100000) ? 5 :
  14. (b >= 10000) ? 4 :
  15. (b >= 1000) ? 3 :
  16. (b >= 100) ? 2 :
  17. (b >= 10) ? 1 : 0 ) {
  18. case 1: return a*100+b; break;
  19. case 2: return a*1000+b; break;
  20. case 3: return a*10000+b; break;
  21. case 4: return a*100000+b; break;
  22. case 5: return a*1000000+b; break;
  23. case 6: return a*10000000+b; break;
  24. case 7: return a*100000000+b; break;
  25. default: return a*10+b; break;
  26. // I don't really know what to do here
  27. //case 8: return a*1000*1000*1000+b; break;
  28. //case 9: return a*10*1000*1000*1000+b; break;
  29. }
  30. }
  31.  
  32. unsigned int uintcat (unsigned int& ms, unsigned int& ls) {
  33. unsigned int mult;
  34. for(mult=10; mult <= ls; mult *= 10) {}
  35. return ms * mult + ls;
  36. }
  37.  
  38. int main() {
  39. default_random_engine generator;
  40. uniform_int_distribution<unsigned int> distrA(0,9),distrB(0,100*1000*1000);
  41. int i;
  42.  
  43. int testCnt = 10*1000*1000;
  44. time_t start_time;
  45. cout << setprecision(3) << fixed;
  46.  
  47. vector<unsigned int> a(testCnt), b(testCnt);
  48. for(i = 0; i < testCnt; i++)
  49. a[i] = distrA(generator), b[i] = distrB(generator);
  50.  
  51. start_time = clock();
  52. for(i = 0; i < testCnt; ++i)
  53. myConcat(a[i], b[i]);
  54. cout << (clock()-start_time)/double(CLOCKS_PER_SEC) << "\n";
  55.  
  56. // use separate vectors to overcome memory hashing, but data is the same
  57. vector<unsigned int> c(a.begin(), a.end()), d(b.begin(), b.end());
  58. start_time = clock();
  59. for(i = 0; i < testCnt; ++i)
  60. uintcat(c[i], d[i]);
  61. cout << (clock()-start_time)/double(CLOCKS_PER_SEC) << "\n";
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 1.38s 3344KB
stdin
Standard input is empty
stdout
0.000
0.100