• Source
    1. /*******************************************************************************
    2.  * Code purpose: performance comparsion std::sort vs std::sort_heap;
    3.  * Author: Copyright(c) 2013, Taras Sich
    4. *******************************************************************************/
    5.  
    6. #include <stdio.h>
    7. #include <iostream>
    8. #include <algorithm>
    9. #include <vector>
    10. #include <iterator>
    11. using namespace std;
    12.  
    13. typedef void (*func_sort)(vector<int>::iterator first, vector<int>::iterator last);
    14.  
    15. inline void make_sorted_vector(vector<int> & elements, unsigned int uiCount)
    16. {
    17. for(unsigned int i = 0; i < uiCount; ++i)
    18. {
    19. elements.push_back(i);
    20. }
    21. }
    22.  
    23. inline void make_heap_sort(vector<int>::iterator first, vector<int>::iterator last)
    24. {
    25. make_heap(first, last);
    26. sort_heap(first, last);
    27. }
    28.  
    29. void TestSortFunction(func_sort sortFunction, unsigned int uiElementsCount)
    30. {
    31. /* Test on sorted range */
    32. {
    33. vector<int> elements;
    34. make_sorted_vector(elements, uiElementsCount);
    35.  
    36. clock_t start = clock();
    37. sortFunction(elements.begin(), elements.end());
    38. clock_t end = clock();
    39.  
    40. double consumed_time = (double)(end - start) / (double)CLOCKS_PER_SEC;
    41. printf("Consumed time for sorted range: %lf sec.\n", consumed_time);
    42. }
    43.  
    44. /* Test on reverse sorted range */
    45. {
    46. vector<int> elements;
    47. make_sorted_vector(elements, uiElementsCount);
    48. reverse(elements.begin(), elements.end());
    49. clock_t start = clock();
    50. sortFunction(elements.begin(), elements.end());
    51. clock_t end = clock();
    52.  
    53. double consumed_time = (double)(end - start) / (double)CLOCKS_PER_SEC;
    54. printf("Consumed time for reversed range: %lf sec.\n", consumed_time);
    55. }
    56.  
    57. /* Test on random range */
    58. {
    59. vector<int> elements;
    60. make_sorted_vector(elements, uiElementsCount);
    61. random_shuffle(elements.begin(), elements.end());
    62. clock_t start = clock();
    63. sortFunction(elements.begin(), elements.end());
    64. clock_t end = clock();
    65.  
    66. double consumed_time = (double)(end - start) / (double)CLOCKS_PER_SEC;
    67. printf("Consumed time for random range: %lf sec.\n", consumed_time);
    68. }
    69. }
    70.  
    71. int main()
    72. {
    73. unsigned int uiTestCount = 10000000;
    74. printf("==== Comparing std::sort vs std::sort_heap performance. ===\n");
    75. printf("==== Range types: sorted, reversed, random\t\t===\n");
    76. printf("==== Range size is %u.\t\t\t\t===\n\n",
    77. uiTestCount);
    78.  
    79. printf("==== std::sort results:\n");
    80. TestSortFunction(sort, uiTestCount);
    81. printf("\n");
    82. printf("==== std::make_heap+std::sort_heap results:\n");
    83. TestSortFunction(make_heap_sort, uiTestCount);
    84. }