Very slow sorting (vs. libstdc++)

In my experiments (using clang++ 14), the following code is up to two orders of magnitude faster with libstdc++ than libc++, with the time difference apparently due to the call to std::sort.

Compiled with -O2 and running on an older CPU, it’s the difference between half a second and 80 seconds.

Any idea why there’s such a huge difference?

#include <string>
#include <algorithm>
#include <cstdlib>

int main()
{
    std::string text;
    for (int i = 0; i < 200000; ++i) text += std::to_string(i);
    size_t numChars = text.size();

    int* suffixes = new int[numChars];
    for (size_t i = 0; i < numChars; i++) suffixes[i] = i;
    
    std::sort (suffixes, suffixes+numChars,
               [&](int x, int y) {
                         return std::lexicographical_compare(
                            text.begin()+x,
                            text.end(),
                            text.begin()+y,
                            text.end());} );

}

Context: this is the core of a naive “suffix array” algorithm. We start with a std::string text (here about 1MB long), and create an array containing all suffixes of this string, where each suffix is represented with an int (the index of the character where that suffix begins). The call to sort then sorts these suffixes alphabetically.

The timing difference does not seem to be due to the specific contents of the string text; libc++ is still much slower if the string contains something more interesting like the text of Moby Dick.