fork download
  1. using namespace std;
  2.  
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <time.h>
  7.  
  8. struct Timer
  9. {
  10. Timer() : start(clock()) {}
  11. double elapsed_ms() { return (double)(clock() - start) * 1000 / CLOCKS_PER_SEC; }
  12. private:
  13. clock_t start;
  14. };
  15.  
  16.  
  17. int main(int argc, char ** argv)
  18. {
  19. (void)argc, (void)argv;
  20.  
  21. for (int size = 100 * 1000 * 1000; size < 100 * 1000 * 1000 + 1; size *= 2)
  22. {
  23. vector<double> source;
  24. source.resize(size);
  25. for (int i = 0; i < size; i++)
  26. {
  27. source[i] = i;
  28. }
  29.  
  30. const int trials1 = 2;
  31. const int trials2 = 2;
  32.  
  33. for (int trial1 = 0; trial1 < trials1; trial1++)
  34. {
  35. random_shuffle(source.begin(), source.end());
  36.  
  37. for (int trial2 = 0; trial2 < trials2; trial2++)
  38. {
  39. auto v = source;
  40. Timer t;
  41. sort(v.begin(), v.end());
  42. printf("qsort(%d): %f ms\n", size, t.elapsed_ms());
  43. }
  44.  
  45. for (int trial2 = 0; trial2 < trials2; trial2++)
  46. {
  47. auto v = source;
  48. Timer t;
  49. std::make_heap(v.begin(), v.end());
  50. std::sort_heap(v.begin(), v.end());
  51. printf("heapsort(%d): %f ms\n", size, t.elapsed_ms());
  52. }
  53. }
  54. }
  55.  
  56. return 0;
  57. }
  58.  
Runtime error #stdin #stdout #stderr 0s 4320KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc