C++11 sort benchmark
Posted on October 24, 2012 by Sol

The code for this post is on GitHub: https://github.com/sol-prog/Sort_benchmark.

A good sorting routine is a building block for many numerical algorithms and, because of this, most modern programming languages provide one or more sorting algorithms in their standard library. C++ is not an exception. What varies, from one C++ implementation to another, is the performance of the sorting algorithm.

C++11 lets you use three sorting functions std::qsort, std::sort and std::stable_sort. While std::qsort comes from the C roots of C++, std::sort and std::stable_sort are provided by C++’s Standard Template Library. The new C++11 standard requires that the complexity of std::sort to be in the worst case. Historically, in the C++98 standard std::sort was defined as on average, with a possible worst case scenario of .

Today, we have a number of C++ compilers that implements, at least partially, the C++11 standard and I think it could be an interesting experiment to test their performance with a reasonably large data set. For this experiment, we are going to use three compilers Clang 3.2svn with libc++, GCC 4.7.2 with libstc++ and Visual Studio 2012. The test machine is a quad-core Intel i7 Q740 processor with 16GB of RAM. GCC and Clang were compiled from sources on Ubuntu 12.04.1 64 bits and Visual Studio 2012 was installed on Windows 8 Pro 64 bits.

The experiment will consist in measuring the execution time of sorting a double precision array, V, of size N with all the available sorting routines from a standard C++ compiler: std::sort, std::stable_sort and std::qsort. The C++11 standard also defines the chrono library that could theoretically be used for accurate time measurements. In practice, however, at the time of this writing, the only compiler that fully implements std::chrono::steady_clock() is Clang. In order to use the same measurement technique with all three test compilers, we are going to use the Boost 1.51 implementation of the steady_clock.

The size N of the test array, V, will vary from two to one million random numbers on the unit interval. Let’s start by defining four tests:

  1. V is filled with unordered random data that will be sorted.
  2. V is filled with ordered random data after the first test. We are going to check how much time a sorting algorithm will take to process a sorted input.
  3. Start by reversing the data from V and sort the reversed array. We are going to check how much time a sorting algorithm will take to process an array filled with data in decreasing order.
  4. Fill V with a unique, random value, and check the performance of the sorting algorithms.

We’ll start by implementing a function that will fill an array with random numbers:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
//Fill a vector with random numbers in the range [lower, upper]
void rnd_fill(std::vector<double> &V, const double lower, const double upper, const unsigned int seed) {

    //use the default random engine and an uniform distribution
    std::default_random_engine eng(seed);
    std::uniform_real_distribution<double> distr(lower, upper);

    for( auto &elem : V){
        elem = distr(eng);
    }
}

Next, we’ll need a function for sorting the data with std::sort:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//Use std::sort
double test_sort(std::vector<double> &V) {
    auto start = boost::chrono::steady_clock::now();
    std::sort(std::begin(V), std::end(V));
    auto end = boost::chrono::steady_clock::now();

    auto diff = end - start;
    
    return boost::chrono::duration <double, boost::milli> (diff).count();
}

The code that tests std::stable_sort is similar with the above function (see the Github repository for this project).

std::qsort needs a helper function that will compare two elements:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
//Function that compares two elements
int comp(const void *a, const void *b) {
    double aux = *(double*)a - *(double*)b;
    if(aux < 0) {
        return -1;
    } 
    else if(aux > 0) {
        return 1;
    }
    return 0;
}

and now the actual code that tests std::qsort:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//Use std::qsort
double test_qsort(std::vector<double> &V) {
    auto start = boost::chrono::steady_clock::now();
    std::qsort(&V[0], V.size(), sizeof(double), comp);
    auto end = boost::chrono::steady_clock::now();

    auto diff = end - start;
    
    return boost::chrono::duration <double, boost::milli> (diff).count();
}

As I’ve said before, the size N of the array V will be increased from two to one million elements, each of the four proposed tests will be repeated for the available sorting algorithms. Each particular test will be repeated one hundred times and the averaged results, with their standard deviation error bars, will be plotted.

In the next three figures, we present the performance of std::sort for the three compilers, for each of the four input cases:

sort_clang

sort_gcc

sort_vs

From the three compilers used for this experiment, Clang seems to have better performance for std::sort with less than 100 milliseconds processing time for about one million random numbers. Clang also has better results than the other two compilers for sorting already sorted data, less than 10 milliseconds. Visual Studio has similar results. GCC, on the other hand, has problems sorting the reversed array.

Next set of results is for std::stable_sort. For this algorithm we have similar results for all three compilers. Notably, GCC performs much better than for std::sort.

stable_sort_clang

stable_sort_gcc

stable_sort_vs

The last set of results for std::qsort is presented in the next three figures:

qsort_clang

qsort_gcc

qsort_vs

For std::qsort, Clang and GCC have similar performance, while Visual Studio has a slightly worse time.

In the next table, we present the size in bytes of the executable generated by each compiler for each algorithm:

Algorithm Compiler
Clang GCC Visual Studio
std::sort 24758 34924 28672
std::stable_sort 24758 31086 32768
std::qsort 24758 30670 28672


If you are interested in learning more about the C++11 Standard Library, I would recommend reading The C++ Standard Library: A Tutorial and Reference (2nd Edition) by N. M. Josuttis:

or Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

blog comments powered by Disqus